Search in sources :

Example 1 with TLSException

use of org.voltcore.network.TLSException in project voltdb by VoltDB.

the class TLSMessagingChannel method writeMessage.

@Override
public int writeMessage(ByteBuffer message) throws IOException {
    if (!message.hasRemaining()) {
        return 0;
    }
    CompositeByteBuf outbuf = Unpooled.compositeBuffer();
    ByteBuf msg = Unpooled.wrappedBuffer(message);
    final int needed = CipherExecutor.framesFor(msg.readableBytes());
    for (int have = 0; have < needed; ++have) {
        final int slicesz = Math.min(CipherExecutor.FRAME_SIZE, msg.readableBytes());
        ByteBuf clear = msg.readSlice(slicesz).writerIndex(slicesz);
        ByteBuf encr = m_ce.allocator().ioBuffer(packetBufferSize()).writerIndex(packetBufferSize());
        ByteBuffer src = clear.nioBuffer();
        ByteBuffer dst = encr.nioBuffer();
        try {
            m_encrypter.tlswrap(src, dst);
        } catch (TLSException e) {
            outbuf.release();
            encr.release();
            throw new IOException("failed to encrypt tls frame", e);
        }
        assert !src.hasRemaining() : "encryption wrap did not consume the whole source buffer";
        encr.writerIndex(dst.limit());
        outbuf.addComponent(true, encr);
    }
    int bytesWritten = 0;
    try {
        while (outbuf.isReadable()) {
            bytesWritten += outbuf.readBytes(m_socketChannel, outbuf.readableBytes());
        }
    } catch (IOException e) {
        throw e;
    } finally {
        outbuf.release();
    }
    message.position(message.position() + msg.readerIndex());
    return bytesWritten;
}
Also used : CompositeByteBuf(io.netty_voltpatches.buffer.CompositeByteBuf) TLSException(org.voltcore.network.TLSException) IOException(java.io.IOException) CompositeByteBuf(io.netty_voltpatches.buffer.CompositeByteBuf) ByteBuf(io.netty_voltpatches.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer)

Example 2 with TLSException

use of org.voltcore.network.TLSException in project voltdb by VoltDB.

the class SSLBufferDecrypter method tlsunwrap.

public int tlsunwrap(ByteBuffer srcBuffer, ByteBuffer dstBuffer) {
    while (true) {
        SSLEngineResult result = null;
        ByteBuffer slice = dstBuffer.slice();
        try {
            result = m_sslEngine.unwrap(srcBuffer, slice);
        } catch (SSLException | ReadOnlyBufferException | IllegalArgumentException | IllegalStateException e) {
            throw new TLSException("ssl engine unwrap fault", e);
        }
        switch(result.getStatus()) {
            case OK:
                if (result.bytesProduced() == 0 && !srcBuffer.hasRemaining()) {
                    return 0;
                }
                // in m_dstBuffer, newly decrtyped data is between pos and lim
                if (result.bytesProduced() > 0) {
                    dstBuffer.limit(dstBuffer.position() + result.bytesProduced());
                    return result.bytesProduced();
                } else {
                    continue;
                }
            case BUFFER_OVERFLOW:
                throw new TLSException("SSL engine unexpectedly overflowed when decrypting");
            case BUFFER_UNDERFLOW:
                throw new TLSException("SSL engine unexpectedly underflowed when decrypting");
            case CLOSED:
                throw new TLSException("SSL engine is closed on ssl unwrap of buffer.");
        }
    }
}
Also used : ReadOnlyBufferException(java.nio.ReadOnlyBufferException) SSLEngineResult(javax.net.ssl.SSLEngineResult) TLSException(org.voltcore.network.TLSException) ByteBuffer(java.nio.ByteBuffer) SSLException(javax.net.ssl.SSLException)

Aggregations

ByteBuffer (java.nio.ByteBuffer)2 TLSException (org.voltcore.network.TLSException)2 ByteBuf (io.netty_voltpatches.buffer.ByteBuf)1 CompositeByteBuf (io.netty_voltpatches.buffer.CompositeByteBuf)1 IOException (java.io.IOException)1 ReadOnlyBufferException (java.nio.ReadOnlyBufferException)1 SSLEngineResult (javax.net.ssl.SSLEngineResult)1 SSLException (javax.net.ssl.SSLException)1