Search in sources :

Example 66 with SSLEngineResult

use of javax.net.ssl.SSLEngineResult in project rabbitmq-java-client by rabbitmq.

the class SslEngineHelper method write.

public static void write(WritableByteChannel socketChannel, SSLEngine engine, ByteBuffer plainOut, ByteBuffer cypherOut) throws IOException {
    while (plainOut.hasRemaining()) {
        cypherOut.clear();
        SSLEngineResult result = engine.wrap(plainOut, cypherOut);
        switch(result.getStatus()) {
            case OK:
                cypherOut.flip();
                while (cypherOut.hasRemaining()) {
                    socketChannel.write(cypherOut);
                }
                break;
            case BUFFER_OVERFLOW:
                throw new SSLException("Buffer overflow occured after a wrap.");
            case BUFFER_UNDERFLOW:
                throw new SSLException("Buffer underflow occured after a wrap.");
            case CLOSED:
                throw new SSLException("Buffer closed");
            default:
                throw new IllegalStateException("Invalid SSL status: " + result.getStatus());
        }
    }
}
Also used : SSLEngineResult(javax.net.ssl.SSLEngineResult) SSLException(javax.net.ssl.SSLException)

Example 67 with SSLEngineResult

use of javax.net.ssl.SSLEngineResult in project ignite by apache.

the class BlockingSslHandler method unwrapHandshake.

/**
 * Unwraps handshake data and processes it.
 *
 * @return Status.
 * @throws SSLException If SSL exception occurred while unwrapping.
 * @throws GridNioException If failed to pass event to the next filter.
 */
private Status unwrapHandshake() throws SSLException, IgniteCheckedException {
    // Flip input buffer so we can read the collected data.
    readFromNet();
    inNetBuf.flip();
    SSLEngineResult res = unwrap0();
    handshakeStatus = res.getHandshakeStatus();
    checkStatus(res);
    // try to unwrap more
    if (handshakeStatus == FINISHED && res.getStatus() == OK && inNetBuf.hasRemaining()) {
        res = unwrap0();
        handshakeStatus = res.getHandshakeStatus();
        // prepare to be written again
        inNetBuf.compact();
        renegotiateIfNeeded(res);
    } else if (res.getStatus() == BUFFER_UNDERFLOW) {
        inNetBuf.compact();
        inNetBuf = expandBuffer(inNetBuf, inNetBuf.capacity() * 2);
    } else
        // prepare to be written again
        inNetBuf.compact();
    return res.getStatus();
}
Also used : SSLEngineResult(javax.net.ssl.SSLEngineResult)

Example 68 with SSLEngineResult

use of javax.net.ssl.SSLEngineResult in project ignite by apache.

the class BlockingSslHandler method unwrapData.

/**
 * Unwraps user data to the application buffer.
 *
 * @throws SSLException If failed to process SSL data.
 * @throws GridNioException If failed to pass events to the next filter.
 */
private void unwrapData() throws IgniteCheckedException, SSLException {
    if (log.isDebugEnabled())
        log.debug("Unwrapping received data.");
    // Flip buffer so we can read it.
    inNetBuf.flip();
    SSLEngineResult res = unwrap0();
    // prepare to be written again
    inNetBuf.compact();
    checkStatus(res);
    renegotiateIfNeeded(res);
}
Also used : SSLEngineResult(javax.net.ssl.SSLEngineResult)

Example 69 with SSLEngineResult

use of javax.net.ssl.SSLEngineResult in project ignite by apache.

the class GridNioSslHandler method unwrapData.

/**
 * Unwraps user data to the application buffer.
 *
 * @throws SSLException If failed to process SSL data.
 * @throws GridNioException If failed to pass events to the next filter.
 */
private void unwrapData() throws IgniteCheckedException, SSLException {
    if (log.isDebugEnabled())
        log.debug("Unwrapping received data: " + ses);
    // Flip buffer so we can read it.
    inNetBuf.flip();
    SSLEngineResult res = unwrap0();
    // prepare to be written again
    inNetBuf.compact();
    checkStatus(res);
    renegotiateIfNeeded(res);
}
Also used : SSLEngineResult(javax.net.ssl.SSLEngineResult)

Example 70 with SSLEngineResult

use of javax.net.ssl.SSLEngineResult in project ignite by apache.

the class GridNioSslHandler method unwrapHandshake.

/**
 * Unwraps handshake data and processes it.
 *
 * @return Status.
 * @throws SSLException If SSL exception occurred while unwrapping.
 * @throws GridNioException If failed to pass event to the next filter.
 */
private Status unwrapHandshake() throws SSLException, IgniteCheckedException {
    // Flip input buffer so we can read the collected data.
    inNetBuf.flip();
    SSLEngineResult res = unwrap0();
    handshakeStatus = res.getHandshakeStatus();
    checkStatus(res);
    // try to unwrap more
    if (handshakeStatus == FINISHED && res.getStatus() == Status.OK && inNetBuf.hasRemaining()) {
        res = unwrap0();
        handshakeStatus = res.getHandshakeStatus();
        // prepare to be written again
        inNetBuf.compact();
        renegotiateIfNeeded(res);
    } else
        // prepare to be written again
        inNetBuf.compact();
    return res.getStatus();
}
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