Search in sources :

Example 11 with HandshakeStatus

use of javax.net.ssl.SSLEngineResult.HandshakeStatus in project webpieces by deanhiller.

the class AsyncSSLEngine2Impl method feedEncryptedPacketImpl.

private void feedEncryptedPacketImpl(ByteBuffer encryptedInData) {
    SSLEngine sslEngine = mem.getEngine();
    HandshakeStatus hsStatus = sslEngine.getHandshakeStatus();
    Status status = null;
    final HandshakeStatus hsStatus2 = hsStatus;
    log.trace(() -> mem + "[sockToEngine] going to unwrap pos=" + encryptedInData.position() + " lim=" + encryptedInData.limit() + " hsStatus=" + hsStatus2 + " cached=" + mem.getCachedToProcess());
    ByteBuffer encryptedData = encryptedInData;
    ByteBuffer cached = mem.getCachedToProcess();
    if (cached != null) {
        encryptedData = combine(cached, encryptedData);
        mem.setCachedEncryptedData(null);
    }
    int i = 0;
    //3. have enough data in buffer(ie. not underflow)
    while (encryptedData.hasRemaining() && status != Status.BUFFER_UNDERFLOW && status != Status.CLOSED) {
        i++;
        SSLEngineResult result;
        ByteBuffer outBuffer = mem.getCachedOut();
        try {
            result = sslEngine.unwrap(encryptedData, outBuffer);
        } catch (SSLException e) {
            AsyncSSLEngineException ee = new AsyncSSLEngineException("status=" + status + " hsStatus=" + hsStatus + " b=" + encryptedData, e);
            throw ee;
        } finally {
            if (outBuffer.position() != 0) {
                outBuffer.flip();
                listener.packetUnencrypted(outBuffer);
                //frequently the out buffer is not used so we only ask the pool for buffers AFTER it has been consumed/used
                ByteBuffer newCachedOut = pool.nextBuffer(sslEngine.getSession().getApplicationBufferSize());
                mem.setCachedOut(newCachedOut);
            }
        }
        status = result.getStatus();
        hsStatus = result.getHandshakeStatus();
        final ByteBuffer data = encryptedData;
        final Status status2 = status;
        final HandshakeStatus hsStatus3 = hsStatus;
        log.trace(() -> mem + "[sockToEngine] unwrap done pos=" + data.position() + " lim=" + data.limit() + " status=" + status2 + " hs=" + hsStatus3);
        if (i > 1000)
            throw new RuntimeException(this + "Bug, stuck in loop, bufIn=" + encryptedData + " bufOut=" + outBuffer + " hsStatus=" + hsStatus + " status=" + status);
        else if (hsStatus == HandshakeStatus.NEED_TASK) {
            //messages?
            break;
        } else if (status == Status.BUFFER_UNDERFLOW) {
            final ByteBuffer data1 = encryptedData;
            log.trace(() -> "buffer underflow. data=" + data1.remaining());
        }
    }
    if (encryptedData.hasRemaining()) {
        mem.setCachedEncryptedData(encryptedData);
    }
    final ByteBuffer data2 = encryptedData;
    final Status status2 = status;
    final HandshakeStatus hsStatus3 = hsStatus;
    log.trace(() -> mem + "[sockToEngine] reset pos=" + data2.position() + " lim=" + data2.limit() + " status=" + status2 + " hs=" + hsStatus3);
    cleanAndFire(hsStatus, status, encryptedData);
}
Also used : HandshakeStatus(javax.net.ssl.SSLEngineResult.HandshakeStatus) Status(javax.net.ssl.SSLEngineResult.Status) SSLEngineResult(javax.net.ssl.SSLEngineResult) SSLEngine(javax.net.ssl.SSLEngine) AsyncSSLEngine(org.webpieces.ssl.api.AsyncSSLEngine) AsyncSSLEngineException(org.webpieces.ssl.api.AsyncSSLEngineException) ByteBuffer(java.nio.ByteBuffer) SSLException(javax.net.ssl.SSLException) HandshakeStatus(javax.net.ssl.SSLEngineResult.HandshakeStatus)

Aggregations

HandshakeStatus (javax.net.ssl.SSLEngineResult.HandshakeStatus)11 ByteBuffer (java.nio.ByteBuffer)7 SSLEngineResult (javax.net.ssl.SSLEngineResult)7 SSLEngine (javax.net.ssl.SSLEngine)5 AsyncSSLEngine (org.webpieces.ssl.api.AsyncSSLEngine)5 Status (javax.net.ssl.SSLEngineResult.Status)4 SSLException (javax.net.ssl.SSLException)2 ByteBuf (io.netty.buffer.ByteBuf)1 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)1 IOException (java.io.IOException)1 SelectionKey (java.nio.channels.SelectionKey)1 Selector (java.nio.channels.Selector)1 ArrayList (java.util.ArrayList)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 SSLSession (javax.net.ssl.SSLSession)1 AsyncSSLEngineException (org.webpieces.ssl.api.AsyncSSLEngineException)1