本文整理汇总了Java中javax.net.ssl.SSLEngineResult.HandshakeStatus.NEED_WRAP属性的典型用法代码示例。如果您正苦于以下问题:Java HandshakeStatus.NEED_WRAP属性的具体用法?Java HandshakeStatus.NEED_WRAP怎么用?Java HandshakeStatus.NEED_WRAP使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类javax.net.ssl.SSLEngineResult.HandshakeStatus
的用法示例。
在下文中一共展示了HandshakeStatus.NEED_WRAP属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: close
/**
* Sends a SSL close message, will not physically close the connection here.<br>
* To close the connection, you could do something like
* <pre><code>
* close();
* while (isOpen() && !myTimeoutFunction()) Thread.sleep(25);
* if ( isOpen() ) close(true); //forces a close if you timed out
* </code></pre>
* @throws IOException if an I/O error occurs
* @throws IOException if there is data on the outgoing network buffer and we are unable to flush it
* TODO Implement this java.io.Closeable method
*/
@Override
public void close() throws IOException {
if (closing) return;
closing = true;
sslEngine.closeOutbound();
if (!flush(netOutBuffer)) {
throw new IOException("Remaining data in the network buffer, can't send SSL close message, force a close with close(true) instead");
}
//prep the buffer for the close message
netOutBuffer.clear();
//perform the close, since we called sslEngine.closeOutbound
SSLEngineResult handshake = sslEngine.wrap(getEmptyBuf(), netOutBuffer);
//we should be in a close state
if (handshake.getStatus() != SSLEngineResult.Status.CLOSED) {
throw new IOException("Invalid close state, will not send network data.");
}
//prepare the buffer for writing
netOutBuffer.flip();
//if there is data to be written
flush(netOutBuffer);
//is the channel closed?
closed = (!netOutBuffer.hasRemaining() && (handshake.getHandshakeStatus() != HandshakeStatus.NEED_WRAP));
}
示例2: getHandshakeStatusInternal
private HandshakeStatus getHandshakeStatusInternal() {
if (handshakeFinished) {
return HandshakeStatus.NOT_HANDSHAKING;
}
switch (state) {
case STATE_HANDSHAKE_STARTED:
return pendingStatus(pendingOutboundEncryptedBytes());
case STATE_HANDSHAKE_COMPLETED:
return HandshakeStatus.NEED_WRAP;
case STATE_NEW:
case STATE_MODE_SET:
case STATE_CLOSED:
case STATE_CLOSED_INBOUND:
case STATE_CLOSED_OUTBOUND:
case STATE_READY:
case STATE_READY_HANDSHAKE_CUT_THROUGH:
return HandshakeStatus.NOT_HANDSHAKING;
default:
break;
}
throw new IllegalStateException("Unexpected engine state: " + state);
}
示例3: unwrap
public ByteBuf unwrap(SocketChannel channel, ByteBuf src) throws IOException {
SSLEngine sslEngine = channel.getSSLEngine();
ByteBuf dst = getTempDst(sslEngine);
for (;;) {
dst.clear();
SSLEngineResult result = sslEngine.unwrap(src.nioBuffer(), dst.nioBuffer());
HandshakeStatus handshakeStatus = result.getHandshakeStatus();
synchByteBuf(result, src, dst);
if (handshakeStatus != HandshakeStatus.NOT_HANDSHAKING) {
if (handshakeStatus == HandshakeStatus.NEED_WRAP) {
channel.doFlush(forgeFuture.duplicate());
return null;
} else if (handshakeStatus == HandshakeStatus.NEED_TASK) {
runDelegatedTasks(sslEngine);
continue;
} else if (handshakeStatus == HandshakeStatus.FINISHED) {
channel.finishHandshake(null);
return null;
} else if (handshakeStatus == HandshakeStatus.NEED_UNWRAP) {
return null;
}
}
return gc(channel, dst.flip());
}
}
示例4: getOutboundData
private HandshakeStatus getOutboundData(ByteBuffer dstBB) {
Object msg = outboundList.removeFirst();
assert(msg instanceof ByteBuffer);
ByteBuffer bbIn = (ByteBuffer) msg;
assert(dstBB.remaining() >= bbIn.remaining());
dstBB.put(bbIn);
/*
* If we have more data in the queue, it's either
* a finished message, or an indication that we need
* to call wrap again.
*/
if (hasOutboundDataInternal()) {
msg = outboundList.getFirst();
if (msg == HandshakeStatus.FINISHED) {
outboundList.removeFirst(); // consume the message
return HandshakeStatus.FINISHED;
} else {
return HandshakeStatus.NEED_WRAP;
}
} else {
return null;
}
}
示例5: processHandshake
private synchronized void processHandshake() throws IOException {
if (this.engineResult.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING) {
if (!this.tasks.isEmpty()) {
Iterator<Future<?>> it = this.tasks.iterator();
while (it.hasNext()) {
Future<?> f = (Future) it.next();
if (f.isDone()) {
it.remove();
} else if (isBlocking()) {
consumeFutureUninterruptible(f);
}
}
}
if (this.engineResult.getHandshakeStatus() == HandshakeStatus.NEED_UNWRAP) {
if (!isBlocking() || this.engineStatus == Status.BUFFER_UNDERFLOW) {
this.inCrypt.compact();
if (this.socketChannel.read(this.inCrypt) == -1) {
throw new IOException("connection closed unexpectedly by peer");
}
this.inCrypt.flip();
}
this.inData.compact();
unwrap();
if (this.engineResult.getHandshakeStatus() == HandshakeStatus.FINISHED) {
createBuffers(this.sslEngine.getSession());
}
}
consumeDelegatedTasks();
if (!$assertionsDisabled && this.engineResult.getHandshakeStatus() == HandshakeStatus.NOT_HANDSHAKING) {
throw new AssertionError();
} else if (this.tasks.isEmpty() || this.engineResult.getHandshakeStatus() == HandshakeStatus.NEED_WRAP) {
this.socketChannel.write(wrap(emptybuffer));
if (this.engineResult.getHandshakeStatus() == HandshakeStatus.FINISHED) {
createBuffers(this.sslEngine.getSession());
}
}
}
}
示例6: close
/**
* Sends a SSL close message, will not physically close the connection here.
* <br>
* To close the connection, you could do something like
*
* <pre>
* <code>
* close();
* while (isOpen() && !myTimeoutFunction()) Thread.sleep(25);
* if ( isOpen() ) close(true); //forces a close if you timed out
* </code>
* </pre>
*
* @throws IOException
* if an I/O error occurs
* @throws IOException
* if there is data on the outgoing network buffer and we are
* unable to flush it TODO Implement this java.io.Closeable
* method
*/
@Override
public void close() throws IOException {
if (closing)
return;
closing = true;
sslEngine.closeOutbound();
if (!flush(netOutBuffer)) {
throw new IOException(
"Remaining data in the network buffer, can't send SSL close message, force a close with close(true) instead");
}
// prep the buffer for the close message
netOutBuffer.clear();
// perform the close, since we called sslEngine.closeOutbound
SSLEngineResult handshake = sslEngine.wrap(getEmptyBuf(), netOutBuffer);
// we should be in a close state
if (handshake.getStatus() != SSLEngineResult.Status.CLOSED) {
throw new IOException("Invalid close state, will not send network data.");
}
// prepare the buffer for writing
netOutBuffer.flip();
// if there is data to be written
flush(netOutBuffer);
// is the channel closed?
closed = (!netOutBuffer.hasRemaining() && (handshake.getHandshakeStatus() != HandshakeStatus.NEED_WRAP));
}
示例7: status
@Override HandshakeStatus status()
{
if (!tasks.isEmpty())
return HandshakeStatus.NEED_TASK;
if (state.isReadState())
return HandshakeStatus.NEED_UNWRAP;
if (state.isWriteState())
return HandshakeStatus.NEED_WRAP;
return HandshakeStatus.FINISHED;
}
示例8: status
@Override HandshakeStatus status()
{
if (state.isReadState())
return HandshakeStatus.NEED_UNWRAP;
if (state.isWriteState())
return HandshakeStatus.NEED_WRAP;
return HandshakeStatus.FINISHED;
}
示例9: handShakeWrapIfNeeded
public static boolean handShakeWrapIfNeeded(SSLConnection cc, Pipe<NetPayloadSchema> target, ByteBuffer buffer, boolean isServer, long arrivalTime) {
HandshakeStatus handshakeStatus = cc.getEngine().getHandshakeStatus();
boolean didShake = false;
while (HandshakeStatus.NOT_HANDSHAKING != handshakeStatus && HandshakeStatus.FINISHED != handshakeStatus ) {
didShake = true;
if (HandshakeStatus.NEED_UNWRAP == handshakeStatus) {
if (cc.durationWaitingForNetwork() > HANDSHAKE_TIMEOUT) {
logger.warn("Handshake wrap abanonded for {} due to timeout of {} ms waiting for unwrap done by reading stage.",cc,HANDSHAKE_TIMEOUT/1000000);
cc.close();
}
return true;//done by the other stage.
}
if (HandshakeStatus.NEED_WRAP == handshakeStatus) {
SSLUtil.handshakeWrapLogic(cc, target, buffer, isServer, arrivalTime);
handshakeStatus = cc.getEngine().getHandshakeStatus();
}
if (HandshakeStatus.NEED_TASK == handshakeStatus) {
Runnable task;
while ((task = cc.getEngine().getDelegatedTask()) != null) {
task.run(); //NOTE: could be run in parallel but we only have 1 thread now
}
handshakeStatus = cc.getEngine().getHandshakeStatus();
//return (HandshakeStatus.NOT_HANDSHAKING != handshakeStatus) && (HandshakeStatus.FINISHED != handshakeStatus);
}
}
cc.clearWaitingForNetwork();
// logger.info("server {} wrap status is now {} for id {} ",isServer,handshakeStatus, cc.getId());
return didShake;
}
示例10: handshakeWrapLogic
public static void handshakeWrapLogic(SSLConnection cc, Pipe<NetPayloadSchema> target, ByteBuffer buffer, boolean isServer, long arrivalTime) {
try {
do {
if (!Pipe.hasRoomForWrite(target)) {
return; //unable to complete, try again later
}
final ByteBuffer[] targetBuffers = Pipe.wrappedWritingBuffers(Pipe.storeBlobWorkingHeadPosition(target), target);
final Status status = SSLUtil.wrapResultStatusState(target, buffer, cc, noDatas, targetBuffers, isServer, arrivalTime);
if (Status.OK == status) {
Pipe.confirmLowLevelWrite(target, Pipe.sizeOf(target, NetPayloadSchema.MSG_ENCRYPTED_200));
Pipe.publishWrites(target);
} else {
//connection was closed before handshake completed
//already closed, NOTE we should release this from reserved pipe pools
//no need to cancel wrapped buffer it was already done by wrapResultStatusState
cc.close();
if (Status.CLOSED != status) {
//not expected case so log this
logger.warn("HANDSHAKE unable to wrap {} {} {} ",status, cc.getClass().getSimpleName(), cc.getEngine(), new Exception());
}
return;
}
} while(cc.getEngine().getHandshakeStatus() == HandshakeStatus.NEED_WRAP);
} catch (SSLException e) {
//logger.error("unable to wrap ", e);
Pipe.unstoreBlobWorkingHeadPosition(target);
}
}
示例11: status
@Override HandshakeStatus status()
{
if (!tasks.isEmpty())
return HandshakeStatus.NEED_TASK;
if (state.isReadState())
return HandshakeStatus.NEED_UNWRAP;
if (state.isWriteState())
return HandshakeStatus.NEED_WRAP;
return HandshakeStatus.FINISHED;
}
示例12: shutdown
boolean shutdown() throws IOException {
if (!shutdown) {
sslEngine.closeOutbound();
shutdown = true;
}
if (outNetBB.hasRemaining() && tryFlush(outNetBB)) {
return false;
}
/*
* By RFC 2616, we can "fire and forget" our close_notify
* message, so that's what we'll do here.
*/
outNetBB.clear();
SSLEngineResult result = sslEngine.wrap(hsBB, outNetBB);
if (result.getStatus() != Status.CLOSED) {
throw new SSLException("Improper close state");
}
outNetBB.flip();
/*
* We won't wait for a select here, but if this doesn't work,
* we'll cycle back through on the next select.
*/
if (outNetBB.hasRemaining()) {
tryFlush(outNetBB);
}
return (!outNetBB.hasRemaining() &&
(result.getHandshakeStatus() != HandshakeStatus.NEED_WRAP));
}
示例13: wrapHS
private int wrapHS(final Buffer pool, final int offset, final int length) throws Exception {
int encLength = 0;
if (sslEngine.getHandshakeStatus() == HandshakeStatus.NEED_WRAP) {
initWrap();
do {
// Generate handshaking data
localNetDataForPeer.clear();
final SSLEngineResult res = sslEngine.wrap(localAppData, localNetDataForPeer);
LOG.log("--HS-- wrap " + res + ", buffer to fill " + pool);
switch (res.getStatus()) {
case OK:
encLength += fillBuffer(localNetDataForPeer, pool);
localNetDataForPeer.compact();
break;
case CLOSED: {
shutdown();
break;
}
case BUFFER_OVERFLOW: {
localNetDataForPeer = handleBufferOverFlow(sslEngine.getSession().getPacketBufferSize(), localNetDataForPeer);
break;
}
case BUFFER_UNDERFLOW: {
throw new RuntimeException("NOT EXPECTING THIS BUFFER_UNDERFLOW");
}
}
if (res.getHandshakeStatus() == HandshakeStatus.FINISHED) {
LOG.log("--HS-- FINISHED, Cipher suite [ " + getCipherSuite() + "] Session [" + HexUtils.encode(sslEngine.getSession().getId()) + "]");
}
// Loop must stop when the provided buffer is full.
} while (sslEngine.getHandshakeStatus() == HandshakeStatus.NEED_WRAP && encLength < length);
}
otherStates();
return encLength;
}
示例14: shutdown
public synchronized boolean shutdown() throws IOException
{
shutdown = true;
if (!engine.isOutboundDone())
{
engine.closeOutbound();
}
// Try to "fire-and-forget" the closed notification (RFC2616).
SSLEngineResult result;
if (prepare(outputBuffer, minBufferSize))
{
result = engine.wrap(emptyBuffer, outputBuffer[0]);
if (result.getStatus() != Status.CLOSED)
{
throw new SSLException("Unexpected shutdown status '"
+ result.getStatus() + '\'');
}
outputBuffer[0].flip();
}
else
{
result = null;
}
flush(outputBuffer[0]);
return !outputBuffer[0].hasRemaining() && (result != null)
&& (result.getHandshakeStatus() != HandshakeStatus.NEED_WRAP);
}
示例15: processHandshake
/**
* This method will do whatever necessary to process the sslengine request.
* Thats why it's called both from the {@link #read(ByteBuffer)} and {@link #write(ByteBuffer)}
**/
private synchronized void processHandshake() throws IOException {
if( sslEngine.getHandshakeStatus() == HandshakeStatus.NOT_HANDSHAKING )
return; // since this may be called either from a reading or a writing thread and because this method is synchronized it is necessary to double check if we are still handshaking.
if( !tasks.isEmpty() ) {
Iterator<Future<?>> it = tasks.iterator();
while ( it.hasNext() ) {
Future<?> f = it.next();
if( f.isDone() ) {
it.remove();
} else {
if( isBlocking() )
consumeFutureUninterruptible( f );
return;
}
}
}
if( sslEngine.getHandshakeStatus() == HandshakeStatus.NEED_UNWRAP ) {
if( !isBlocking() || readEngineResult.getStatus() == Status.BUFFER_UNDERFLOW ) {
inCrypt.compact();
int read = socketChannel.read( inCrypt );
if( read == -1 ) {
throw new IOException( "connection closed unexpectedly by peer" );
}
inCrypt.flip();
}
inData.compact();
unwrap();
if( readEngineResult.getHandshakeStatus() == HandshakeStatus.FINISHED ) {
createBuffers( sslEngine.getSession() );
return;
}
}
consumeDelegatedTasks();
if( tasks.isEmpty() || sslEngine.getHandshakeStatus() == HandshakeStatus.NEED_WRAP ) {
socketChannel.write( wrap( emptybuffer ) );
if( writeEngineResult.getHandshakeStatus() == HandshakeStatus.FINISHED ) {
createBuffers( sslEngine.getSession() );
return;
}
}
assert ( sslEngine.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING );// this function could only leave NOT_HANDSHAKING after createBuffers was called unless #190 occurs which means that nio wrap/unwrap never return HandshakeStatus.FINISHED
bufferallocations = 1; // look at variable declaration why this line exists and #190. Without this line buffers would not be be recreated when #190 AND a rehandshake occur.
}