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;
}
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.");
}
}
}
Aggregations