/*
* 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-2013 ForgeRock AS
*/
/**
* A class that provides a TLS byte channel implementation.
*/
{
/**
* Private implementation.
*/
{
/**
* {@inheritDoc}
*/
{
synchronized (readLock)
{
synchronized (writeLock)
{
try
{
if (!sslEngine.isOutboundDone())
{
{
// Write out any remaining SSL close notifications.
}
}
}
catch (final ClosedChannelException e)
{
// Ignore this so that close is idempotent.
}
finally
{
try
{
}
catch (final SSLException e)
{
// Not yet received peer's close notification. Ignore this if we
// are the initiator.
if (!isInitiator)
{
throw e;
}
}
finally
{
}
}
}
}
}
/**
* {@inheritDoc}
*/
public boolean isOpen()
{
}
/**
* {@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)
{
// Repeat until the entire input data is written.
while (unwrappedData.hasRemaining())
{
// Wrap and send the data.
// Perform handshake if needed.
{
doHandshake(false /* isReading */);
}
}
}
return bytesWritten;
}
// It seems that the SSL engine does not remember if an error has already
// occurred so we must cache it here and rethrow. See OPENDJ-652.
{
if (sslException != null)
{
throw sslException;
}
}
{
// This lock is probably unnecessary since tasks can be run in parallel,
// but it adds no additional overhead so there's little harm in having
// it.
synchronized (handshakeLock)
{
while (true)
{
switch (sslEngine.getHandshakeStatus())
{
case NEED_TASK:
{
}
break;
case NEED_UNWRAP:
// Block for writes, but be non-blocking for reads.
if (isReading)
{
// Let doRecvAndUnwrap() deal with this.
return;
}
// Need to do an unwrap (read) while writing.
if (doRecvAndUnwrap() < 0)
{
throw new ClosedChannelException();
}
break;
case NEED_WRAP:
break;
default: // NOT_HANDSHAKING, FINISHED.
return;
}
}
}
}
// Attempt to read and unwrap the next SSL packet.
{
// Synchronize SSL unwrap with channel reads.
synchronized (unwrapLock)
{
// Read SSL packets until some unwrapped data is produced or no more
// data is available on the underlying channel.
while (true)
{
// Unwrap any remaining data in the buffer.
final SSLEngineResult result;
try
{
}
catch (final SSLException e)
{
// Save the error - see abortOnSSLException().
sslException = e;
throw e;
}
finally
{
}
{
case BUFFER_OVERFLOW:
// The unwrapped buffer is not big enough: resize and repeat.
break; // Retry unwrap.
case BUFFER_UNDERFLOW:
// Not enough data was read. This either means that the inbound
// buffer was too small, or not enough data was read.
{
// Increase the buffer size.
}
// Read wrapped data from underlying channel.
if (read <= 0)
{
// Not enough data is available to read a complete SSL packet, or
// channel closed.
return read;
}
// Loop and unwrap.
break;
case CLOSED:
// Peer sent SSL close notification.
return -1;
default: // OK
if (recvUnwrappedBuffer.hasRemaining())
{
// Some application data was read so return it.
return recvUnwrappedBuffer.remaining();
}
{
// No application data was read, but if we are handshaking then
// try to continue.
doHandshake(true /* isReading */);
}
break;
}
}
}
}
// Attempt to wrap and send the next SSL packet.
throws IOException
{
// Synchronize SSL wrap with channel writes.
synchronized (wrapLock)
{
// Repeat while there is overflow.
while (true)
{
final SSLEngineResult result;
try
{
}
catch (SSLException e)
{
// Save the error - see abortOnSSLException().
sslException = e;
throw e;
}
{
case BUFFER_OVERFLOW:
// The wrapped buffer is not big enough: resize and repeat.
break; // Retry.
case BUFFER_UNDERFLOW:
// This should not happen for sends.
new SSLException("Got unexpected underflow while wrapping");
throw sslException;
case CLOSED:
throw new ClosedChannelException();
default: // OK
// Write the SSL packet: our IO stack will block until all the
// data is written.
while (sendWrappedBuffer.hasRemaining())
{
}
return written;
}
}
}
}
{
}
}
/**
* Map of cipher phrases to effective key size (bits). Taken from the
* following RFCs: 5289, 4346, 3268,4132 and 4162 and the IANA Transport Layer
* Security (TLS) Parameters.
*
* @see <a
* Transport Layer Security (TLS) Parameters, TLS Cipher Suite Registry</a>
*/
static
{
}
/**
* Creates an TLS byte channel instance using the specified LDAP connection
* configuration, client connection, SSL context and socket channel
* parameters.
*
* @param channel
* The underlying channel.
* @param sslEngine
* The SSL engine to use.
*/
{
// Initially nothing has been received.
}
/**
* {@inheritDoc}
*/
{
return pimpl;
}
/**
* {@inheritDoc}
*/
{
try
{
}
catch (final SSLPeerUnverifiedException e)
{
if (debugEnabled())
{
}
return new Certificate[0];
}
}
/**
* {@inheritDoc}
*/
{
return "TLS";
}
/**
* {@inheritDoc}
*/
public int getSSF()
{
{
}
return 0;
}
/**
* Returns the Security Strength Factor corresponding to the supplied cipher
* string.
*
* @param cipherString
* the cipher to test for SSF
* @return the Security Strength Factor corresponding to the supplied cipher
* string, null if the cipher cannot be recognized.
*/
{
{
{
}
}
return null;
}
/**
* {@inheritDoc}
*/
public boolean isSecure()
{
return true;
}
}