/** * Perform an SSL handshake using the SSLEngine. * @param key the <code>SelectionKey</code> * @param byteBuffer The application <code>ByteBuffer</code> * @param inputBB The encrypted input <code>ByteBuffer</code> * @param outputBB The encrypted output <code>ByteBuffer</code> * @param sslEngine The SSLEngine used. * @param handshakeStatus The current handshake status * @param timeout The time the Selector will block waiting for bytes * @return byteBuffer the new ByteBuffer * @throw IOException if the handshake fail. */ public static ByteBuffer doHandshake(SelectionKey key, ByteBuffer byteBuffer, ByteBuffer inputBB, ByteBuffer outputBB, SSLEngine sslEngine, HandshakeStatus handshakeStatus) throws IOException { return doHandshake(key,byteBuffer,inputBB,outputBB,sslEngine, handshakeStatus,readTimeout); }
int byteRead = 0; while (byteBuffer.position() == initialPosition){ int count = SSLUtils.doRead(key,inputBB,sslEngine,readTimeout); if (count > 0) { byteRead += count; try{ byteBuffer = SSLUtils.unwrapAll(byteBuffer,inputBB,sslEngine); } catch (IOException ex){ Logger logger = SSLSelectorThread.logger();
do{ try{ result = unwrap(byteBuffer,inputBB,sslEngine); } catch (Throwable ex){ Logger logger = SSLSelectorThread.logger(); if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) { executeDelegatedTask(sslEngine); byteBuffer = reallocate(byteBuffer); break; default:
/** * Encrypt the response and flush it using <code>OutputWriter</code> */ public static void flushChannel(SocketChannel socketChannel, ByteBuffer bb, ByteBuffer outputBB, SSLEngine sslEngine) throws IOException{ while (bb.hasRemaining()) { SSLEngineResult result = SSLUtils.wrap(bb,outputBB,sslEngine); switch (result.getStatus()) { case OK: if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) { SSLUtils.executeDelegatedTask(sslEngine); } break; default: throw new IOException("SSLOutputWriter: " + result.getStatus()); } if (outputBB.hasRemaining()) { OutputWriter.flushChannel(socketChannel,outputBB); } } outputBB.clear(); } }
switch (handshakeStatus) { case NEED_UNWRAP: if (doRead(key,inputBB,sslEngine, timeout) <= eof) { try{ sslEngine.closeInbound(); result = unwrap(byteBuffer,inputBB,sslEngine); handshakeStatus = result.getHandshakeStatus(); executeDelegatedTask(sslEngine); break; byteBuffer = reallocate(byteBuffer); break; default: result = wrap(hsBB,outputBB,sslEngine); handshakeStatus = result.getHandshakeStatus(); switch (result.getStatus()) { handshakeStatus = executeDelegatedTask(sslEngine);
if (protocolInfo.handshake){ try{ byteBuffer = SSLUtils.doHandshake(key,byteBuffer,inputBB,outputBB, sslEngine,handshakeStatus, SSLUtils.getReadTimeout()); protocolInfo.handshake = false; } catch (EOFException ex) { int readTimeout = SSLUtils.getReadTimeout(); readTimeout = 0; int byteRead = SSLUtils.doRead(key,inputBB,sslEngine, readTimeout); if (byteRead > -1){ protocolInfo.byteBuffer = SSLUtils.unwrapAll(byteBuffer,inputBB,sslEngine); protocolInfo.bytesRead = byteBuffer.position();
private int doRead(ByteBuffer inputBB){ int count = -1; try{ // Read first bytes to avoid continuing if the client // closed the connection. count = ((SocketChannel)key.channel()).read(inputBB); if (count != -1){ // Decrypt the bytes we just read. byteBuffer = SSLUtils.unwrapAll(byteBuffer,inputBB,sslEngine); final SSLWorkerThread workerThread = (SSLWorkerThread)Thread.currentThread(); workerThread.setByteBuffer(byteBuffer); workerThread.setInputBB(inputBB); } return count; } catch(IOException ex){ return -1; } finally { if (count == -1){ try{ sslEngine.closeInbound(); } catch (SSLException ex){ ; } } } }
System.getProperty(TEMPORARY_SELECTOR_TIMEOUT)); ByteBufferInputStream.setDefaultReadTimeout(timeout); SSLUtils.setReadTimeout(timeout); } catch (NumberFormatException ex){ SelectorThread.logger().log(Level.WARNING,
try { allocateBuffers(); if (!doHandshake(SSLUtils.getReadTimeout())) { keepAlive = false; count = -1;
/** * Execute a non blocking SSL handshake. */ protected boolean doHandshake(int timeout) throws IOException{ HandshakeStatus handshakeStatus = HandshakeStatus.NEED_UNWRAP; boolean OK = true; final SSLWorkerThread workerThread = (SSLWorkerThread)Thread.currentThread(); try{ if ( handshake ) { byteBuffer = SSLUtils.doHandshake (key,byteBuffer,inputBB,outputBB,sslEngine, handshakeStatus,timeout); if (doRead(inputBB) == -1){ throw new EOFException(); } } } catch (EOFException ex) { Logger logger = SSLSelectorThread.logger(); if ( logger.isLoggable(Level.FINE) ){ logger.log(Level.FINE,"doHandshake",ex); } OK = false; } finally { workerThread.setOutputBB(outputBB); } return OK; }