Search in sources :

Example 61 with SSLEngineResult

use of javax.net.ssl.SSLEngineResult in project jdk8u_jdk by JetBrains.

the class RehandshakeFinished method runTest.

/*
     * Run the test.
     *
     * Sit in a tight loop, both engines calling wrap/unwrap regardless
     * of whether data is available or not.  We do this until both engines
     * report back they are closed.
     *
     * The main loop handles all of the I/O phases of the SSLEngine's
     * lifetime:
     *
     *     initial handshaking
     *     application data transfer
     *     engine closing
     *
     * One could easily separate these phases into separate
     * sections of code.
     */
private SSLSession runTest() throws Exception {
    boolean dataDone = false;
    createSSLEngines();
    createBuffers();
    // results from client's last operation
    SSLEngineResult clientResult;
    // results from server's last operation
    SSLEngineResult serverResult;
    /*
         * Examining the SSLEngineResults could be much more involved,
         * and may alter the overall flow of the application.
         *
         * For example, if we received a BUFFER_OVERFLOW when trying
         * to write to the output pipe, we could reallocate a larger
         * pipe, but instead we wait for the peer to drain it.
         */
    while (!isEngineClosed(clientEngine) || !isEngineClosed(serverEngine)) {
        log("================");
        clientResult = clientEngine.wrap(clientOut, cTOs);
        log("client wrap: ", clientResult);
        runDelegatedTasks(clientResult, clientEngine);
        serverResult = serverEngine.wrap(serverOut, sTOc);
        log("server wrap: ", serverResult);
        runDelegatedTasks(serverResult, serverEngine);
        cTOs.flip();
        sTOc.flip();
        log("----");
        clientResult = clientEngine.unwrap(sTOc, clientIn);
        log("client unwrap: ", clientResult);
        runDelegatedTasks(clientResult, clientEngine);
        serverResult = serverEngine.unwrap(cTOs, serverIn);
        log("server unwrap: ", serverResult);
        runDelegatedTasks(serverResult, serverEngine);
        cTOs.compact();
        sTOc.compact();
        /*
             * After we've transfered all application data between the client
             * and server, we close the clientEngine's outbound stream.
             * This generates a close_notify handshake message, which the
             * server engine receives and responds by closing itself.
             */
        if (!dataDone && (clientOut.limit() == serverIn.position()) && (serverOut.limit() == clientIn.position())) {
            /*
                 * A sanity check to ensure we got what was sent.
                 */
            checkTransfer(serverOut, clientIn);
            checkTransfer(clientOut, serverIn);
            log("\tClosing clientEngine's *OUTBOUND*...");
            clientEngine.closeOutbound();
            dataDone = true;
        }
    }
    return clientEngine.getSession();
}
Also used : SSLEngineResult(javax.net.ssl.SSLEngineResult)

Example 62 with SSLEngineResult

use of javax.net.ssl.SSLEngineResult in project jdk8u_jdk by JetBrains.

the class SSLEngineDeadlock method runTest.

/*
     * Run the test.
     *
     * Sit in a tight loop, both engines calling wrap/unwrap regardless
     * of whether data is available or not.  We do this until both engines
     * report back they are closed.
     *
     * The main loop handles all of the I/O phases of the SSLEngine's
     * lifetime:
     *
     *     initial handshaking
     *     application data transfer
     *     engine closing
     *
     * One could easily separate these phases into separate
     * sections of code.
     */
private void runTest() throws Exception {
    boolean dataDone = false;
    createSSLEngines();
    createBuffers();
    // results from client's last operation
    SSLEngineResult clientResult;
    // results from server's last operation
    SSLEngineResult serverResult;
    new Thread("SSLEngine Task Dispatcher") {

        public void run() {
            try {
                doTask();
            } catch (Exception e) {
                System.err.println("Task thread died...test will hang");
            }
        }
    }.start();
    /*
         * Examining the SSLEngineResults could be much more involved,
         * and may alter the overall flow of the application.
         *
         * For example, if we received a BUFFER_OVERFLOW when trying
         * to write to the output pipe, we could reallocate a larger
         * pipe, but instead we wait for the peer to drain it.
         */
    while (!isEngineClosed(clientEngine) || !isEngineClosed(serverEngine)) {
        log("================");
        clientResult = clientEngine.wrap(clientOut, cTOs);
        log("client wrap: ", clientResult);
        serverResult = serverEngine.wrap(serverOut, sTOc);
        log("server wrap: ", serverResult);
        cTOs.flip();
        sTOc.flip();
        log("----");
        clientResult = clientEngine.unwrap(sTOc, clientIn);
        log("client unwrap: ", clientResult);
        serverResult = serverEngine.unwrap(cTOs, serverIn);
        log("server unwrap: ", serverResult);
        cTOs.compact();
        sTOc.compact();
        /*
             * After we've transfered all application data between the client
             * and server, we close the clientEngine's outbound stream.
             * This generates a close_notify handshake message, which the
             * server engine receives and responds by closing itself.
             */
        if (!dataDone && (clientOut.limit() == serverIn.position()) && (serverOut.limit() == clientIn.position())) {
            /*
                 * A sanity check to ensure we got what was sent.
                 */
            checkTransfer(serverOut, clientIn);
            checkTransfer(clientOut, serverIn);
            log("\tClosing clientEngine's *OUTBOUND*...");
            clientEngine.closeOutbound();
            dataDone = true;
        }
    }
    testDone = true;
}
Also used : SSLEngineResult(javax.net.ssl.SSLEngineResult)

Example 63 with SSLEngineResult

use of javax.net.ssl.SSLEngineResult in project jdk8u_jdk by JetBrains.

the class RehandshakeFinished method runRehandshake.

private SSLSession runRehandshake() throws Exception {
    log("\n\n==============================================");
    log("Staring actual test.");
    createSSLEngines();
    createBuffers();
    SSLEngineResult result;
    log("Client's ClientHello");
    checkResult(clientEngine, clientEngine.wrap(clientOut, cTOs), HandshakeStatus.NEED_UNWRAP, false, true);
    cTOs.flip();
    checkResult(serverEngine, serverEngine.unwrap(cTOs, serverIn), HandshakeStatus.NEED_WRAP, true, false);
    cTOs.compact();
    log("Server's ServerHello/ServerHelloDone");
    checkResult(serverEngine, serverEngine.wrap(serverOut, sTOc), HandshakeStatus.NEED_WRAP, false, true);
    sTOc.flip();
    checkResult(clientEngine, clientEngine.unwrap(sTOc, clientIn), HandshakeStatus.NEED_UNWRAP, true, false);
    sTOc.compact();
    log("Server's CCS");
    checkResult(serverEngine, serverEngine.wrap(serverOut, sTOc), HandshakeStatus.NEED_WRAP, false, true);
    sTOc.flip();
    checkResult(clientEngine, clientEngine.unwrap(sTOc, clientIn), HandshakeStatus.NEED_UNWRAP, true, false);
    sTOc.compact();
    log("Server's FINISHED");
    checkResult(serverEngine, serverEngine.wrap(serverOut, sTOc), HandshakeStatus.NEED_UNWRAP, false, true);
    sTOc.flip();
    checkResult(clientEngine, clientEngine.unwrap(sTOc, clientIn), HandshakeStatus.NEED_WRAP, true, false);
    sTOc.compact();
    log("Client's CCS");
    checkResult(clientEngine, clientEngine.wrap(clientOut, cTOs), HandshakeStatus.NEED_WRAP, false, true);
    cTOs.flip();
    checkResult(serverEngine, serverEngine.unwrap(cTOs, serverIn), HandshakeStatus.NEED_UNWRAP, true, false);
    cTOs.compact();
    log("Client's FINISHED should trigger FINISHED messages all around.");
    checkResult(clientEngine, clientEngine.wrap(clientOut, cTOs), HandshakeStatus.FINISHED, false, true);
    cTOs.flip();
    checkResult(serverEngine, serverEngine.unwrap(cTOs, serverIn), HandshakeStatus.FINISHED, true, false);
    cTOs.compact();
    return clientEngine.getSession();
}
Also used : SSLEngineResult(javax.net.ssl.SSLEngineResult)

Example 64 with SSLEngineResult

use of javax.net.ssl.SSLEngineResult in project tigervnc by TigerVNC.

the class SSLEngineManager method doHandshake.

public void doHandshake() throws Exception {
    // Begin handshake
    engine.beginHandshake();
    SSLEngineResult.HandshakeStatus hs = engine.getHandshakeStatus();
    // Process handshaking message
    SSLEngineResult res = null;
    int appBufSize = engine.getSession().getApplicationBufferSize();
    ByteBuffer peerAppData = ByteBuffer.allocate(appBufSize);
    ByteBuffer myAppData = ByteBuffer.allocate(appBufSize);
    while (hs != SSLEngineResult.HandshakeStatus.FINISHED && hs != SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING) {
        switch(hs) {
            case NEED_UNWRAP:
                // Receive handshaking data from peer
                peerNetData.flip();
                res = engine.unwrap(peerNetData, peerAppData);
                peerNetData.compact();
                hs = res.getHandshakeStatus();
                // Check status
                switch(res.getStatus()) {
                    case BUFFER_UNDERFLOW:
                        int avail = in.check(1, peerNetData.remaining(), false);
                        in.readBytes(peerNetData, avail);
                        break;
                    case OK:
                        // Process incoming handshaking data
                        break;
                    case CLOSED:
                        engine.closeInbound();
                        break;
                }
                break;
            case NEED_WRAP:
                // Generate handshaking data
                res = engine.wrap(myAppData, myNetData);
                hs = res.getHandshakeStatus();
                // Check status
                switch(res.getStatus()) {
                    case OK:
                        myNetData.flip();
                        os.writeBytes(myNetData, myNetData.remaining());
                        os.flush();
                        myNetData.compact();
                        break;
                    case CLOSED:
                        engine.closeOutbound();
                        break;
                }
                break;
            case NEED_TASK:
                // Handle blocking tasks
                executeTasks();
                break;
        }
        hs = engine.getHandshakeStatus();
    }
}
Also used : SSLEngineResult(javax.net.ssl.SSLEngineResult)

Example 65 with SSLEngineResult

use of javax.net.ssl.SSLEngineResult in project tigervnc by TigerVNC.

the class SSLEngineManager method write.

public int write(ByteBuffer data, int length) throws IOException {
    int n = 0;
    while (data.hasRemaining()) {
        SSLEngineResult res = engine.wrap(data, myNetData);
        n += res.bytesConsumed();
        switch(res.getStatus()) {
            case OK:
                myNetData.flip();
                os.writeBytes(myNetData, myNetData.remaining());
                os.flush();
                myNetData.compact();
                break;
            case BUFFER_OVERFLOW:
                // Make room in the buffer by flushing the outstream
                myNetData.flip();
                os.writeBytes(myNetData, myNetData.remaining());
                myNetData.compact();
                break;
            case CLOSED:
                engine.closeOutbound();
                break;
        }
    }
    return n;
}
Also used : SSLEngineResult(javax.net.ssl.SSLEngineResult)

Aggregations

SSLEngineResult (javax.net.ssl.SSLEngineResult)139 ByteBuffer (java.nio.ByteBuffer)53 IOException (java.io.IOException)32 SSLException (javax.net.ssl.SSLException)32 SSLEngine (javax.net.ssl.SSLEngine)25 Test (org.junit.Test)13 ReadOnlyBufferException (java.nio.ReadOnlyBufferException)12 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)10 HandshakeStatus (javax.net.ssl.SSLEngineResult.HandshakeStatus)9 EOFException (java.io.EOFException)7 ByteBuf (io.netty.buffer.ByteBuf)6 SSLSession (javax.net.ssl.SSLSession)6 WritePendingException (java.nio.channels.WritePendingException)5 KeyManagementException (java.security.KeyManagementException)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 ExecutionException (java.util.concurrent.ExecutionException)5 TimeoutException (java.util.concurrent.TimeoutException)5 Status (javax.net.ssl.SSLEngineResult.Status)5 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)4 BufferUnderflowException (java.nio.BufferUnderflowException)3