/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at legal-notices/CDDLv1_0.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Copyright 2008-2009 Sun Microsystems, Inc.
* Portions Copyright 2012 ForgeRock AS
*/
/**
* This class implements a SASL byte channel that can be used during
* confidentiality and integrity.
*/
{
/**
* Private implementation.
*/
{
/**
* {@inheritDoc}
*/
{
synchronized (readLock)
{
synchronized (writeLock)
{
}
}
}
/**
* {@inheritDoc}
*/
public boolean isOpen()
{
return saslContext != null;
}
/**
* {@inheritDoc}
*/
{
synchronized (readLock)
{
// Only read and unwrap new data if needed.
if (!recvUnwrappedBuffer.hasRemaining())
{
final int read = doRecvAndUnwrap();
if (read <= 0)
{
// No data read or end of stream.
return read;
}
}
// Copy available data.
{
// Unwrapped data does not fit in client buffer so copy one byte at a
// time: it's annoying that there is no easy way to do this with
// ByteBuffers.
while (unwrappedData.hasRemaining())
{
}
}
else
{
// Unwrapped data fits client buffer so block copy.
}
}
}
/**
* {@inheritDoc}
*/
{
// This method will block until the entire message is sent.
// Synchronized in order to prevent interleaving and reordering.
synchronized (writeLock)
{
// Write data in sendBufferSize segments.
while (unwrappedData.hasRemaining())
{
final byte[] wrappedDataBytes;
if (unwrappedData.hasArray())
{
// Avoid extra copy if ByteBuffer is array based.
wrapSize);
}
else
{
// Non-array based ByteBuffer, so copy.
}
// Encode SASL packet: 4 byte length + wrapped data.
{
// Resize the send buffer.
}
// Write the SASL packet: our IO stack will block until all the data
// is written.
}
}
return bytesWritten;
}
// Attempt to read and unwrap the next SASL packet.
{
// Read SASL packets until some unwrapped data is produced or no more
// data is available on the underlying channel.
while (true)
{
// Read the wrapped packet length first.
if (recvWrappedLength < 0)
{
// The channel read may only partially fill the buffer due to
// buffering in the underlying channel layer (e.g. SSL layer), so
// repeatedly read until the length has been read or we are sure
// that we are unable to proceed.
while (recvWrappedLengthBuffer.hasRemaining())
{
if (read <= 0)
{
// Not enough data available or end of stream.
return read;
}
}
// Decode the length and reset the length buffer.
// Check that the length is valid.
{
throw new IOException(
"Client sent a SASL packet specifying a length "
+ " which exceeds the negotiated limit of "
}
if (recvWrappedLength < 0)
{
throw new IOException(
"Client sent a SASL packet specifying a negative length "
}
// Prepare the recv buffer for reading.
}
// Read the wrapped packet data.
// The channel read may only partially fill the buffer due to
// buffering in the underlying channel layer (e.g. SSL layer), so
// repeatedly read until the data has been read or we are sure
// that we are unable to proceed.
while (recvWrappedBuffer.hasRemaining())
{
if (read <= 0)
{
// Not enough data available or end of stream.
return read;
}
}
// The complete packet has been read, so unwrap it.
recvWrappedLength = -1;
// Only return the unwrapped data if it was non-empty, otherwise try to
// read another SASL packet.
{
return recvUnwrappedBuffer.remaining();
}
}
}
}
/**
* Return a SASL byte channel instance created using the specified parameters.
*
* @param c
* A client connection associated with the instance.
* @param name
* The name of the instance (SASL mechanism name).
* @param context
* A SASL context associated with the instance.
* @return A SASL byte channel.
*/
{
}
private final int recvWrappedBufferMaximumSize;
private final int sendUnwrappedBufferSize;
private final byte[] sendUnwrappedBytes;
/**
* Create a SASL byte channel with the specified parameters that is capable of
* processing a confidentiality/integrity SASL connection.
*
* @param connection
* @param name
* The SASL mechanism name.
* @param saslContext
* The SASL context to process the data through.
*/
final SASLContext saslContext)
{
this.saslContext = saslContext;
sendUnwrappedBytes = new byte[sendUnwrappedBufferSize];
}
/**
* {@inheritDoc}
*/
{
return pimpl;
}
/**
* {@inheritDoc}
*/
{
return new Certificate[0];
}
/**
* {@inheritDoc}
*/
{
return name;
}
/**
* {@inheritDoc}
*/
public int getSSF()
{
return saslContext.getSSF();
}
/**
* {@inheritDoc}
*/
public boolean isSecure()
{
return true;
}
}