/** * Create new <code>SSLWorkerThread</code> */ @Override protected void increaseWorkerThread(int increment, boolean startThread){ SSLWorkerThread workerThread; int currentCount = threadCount; int increaseCount = threadCount + increment; for (int i=currentCount; i < increaseCount; i++){ workerThread = new SSLWorkerThread(this, name + "SSLWorkerThread-" + port + "-" + i); workerThread.setPriority(priority); if (startThread) workerThread.start(); workerThreads[i] = workerThread; threadCount++; } } }
/** * Encrypt the response and flush it using <code>OutputWriter</code> */ public static void flushChannel(SocketChannel socketChannel, ByteBuffer bb) throws IOException{ SSLWorkerThread workerThread = (SSLWorkerThread)Thread.currentThread(); SSLEngine sslEngine = workerThread.getSSLEngine(); ByteBuffer outputBB = workerThread.getOutputBB(); flushChannel(socketChannel,bb,outputBB,sslEngine); }
newBB.put(inputBB); inputBB = newBB; } else if (inputBB == null && workerThread.getInputBB() != null ){ inputBB = workerThread.getInputBB(); } else if (inputBB == null){ inputBB = ByteBuffer.allocate(inputBBSize); if (workerThread.getOutputBB() == null) { outputBB = ByteBuffer.allocate(inputBBSize); } else { outputBB = workerThread.getOutputBB(); if (byteBuffer == null && workerThread.getByteBuffer() == null){ byteBuffer = ByteBuffer.allocate(inputBBSize * 2); } else if (byteBuffer == null){ byteBuffer = workerThread.getByteBuffer(); workerThread.setInputBB(inputBB); workerThread.setOutputBB(outputBB); workerThread.setByteBuffer(byteBuffer); workerThread.setSSLEngine(sslEngine);
int currentRead = SSLUtils.doRead(key,workerThread.getInputBB(), workerThread.getSSLEngine(),readTimeout); if (currentRead > 0 || workerThread.getInputBB().position() > 0) { try{ byteBuffer = SSLUtils.unwrapAll(byteBuffer, workerThread.getInputBB(),workerThread.getSSLEngine()); workerThread.setByteBuffer(byteBuffer); if (currentRead == -1 && byteBuffer.position() == initialPosition) {
sslEngine.beginHandshake(); ByteBuffer origBB = workerThread.getByteBuffer(); outputBB = workerThread.getOutputBB(); byteBuffer = origBB; handshake= false; workerThread.setByteBuffer(byteBuffer); inputStream.setByteBuffer(byteBuffer); byteBuffer.clear();
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){ ; } } } }
/** * 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; }