Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

HandShakeStatus

843811Aug 21 2008 — edited Sep 8 2008
I write a NIO Server program with SSL(JSSE).I encount some problem in here.

SSLEngineResult res = sslEngine.wrap(////);
HandshakeStatus hs = res.getHandshakeStatus();
HandshakeStatus hs2 = engine.getHandshakeStatus();


in my program ,hs = FINISH but hs2=NOT_HANDSHAKE,why it isn't same value? and why my program get a NOT_HANDSHAKE status?

my program :


package cn.com.infosec.isfw2.sfw;

import java.io.EOFException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLEngineResult;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLEngineResult.HandshakeStatus;
import javax.net.ssl.SSLEngineResult.Status;

import com.sun.corba.se.pept.transport.OutboundConnectionCache;

import cn.com.infosec.isfw2.impl.DefaultProtocolHandler;

public class SSLSession extends SocketSession {

private SocketChannel channel;

private IOFilterChain filters;

private ByteBuffer buffer = null;

private Selector selector;

// private HashMap<String, Object> attributes;

private SSLEngine sslEngine;

private ProtocolHandler protocolHandler = null;

private ByteBuffer inNetBuffer;
private ByteBuffer outNetBuffer;
//
// /**
// * Applicaton cleartext data to be read by application
// */
// private IoBuffer appBuffer;

/**
* Empty buffer used during initial handshake and close operations
*/
private final ByteBuffer emptyBuffer = ByteBuffer.allocate(0);

private SSLEngineResult.HandshakeStatus handshakeStatus;
private boolean initialHandshakeComplete;
private boolean handshakeComplete;

public SSLSession(SocketChannel channel, Selector sel, IOFilterChain chain) {
super(channel, sel, chain);

this.channel = channel;
this.filters = chain;
this.selector = sel;
buffer = null;
// attributes = null;
filters.SessionCreate(this);

System.out.println("----create ssl session--------");
System.out.println("create session,channel=" + this.channel);

}

public void read() throws IOException {
if (buffer == null) {
buffer = ByteBuffer.allocate(8192);
}

if (buffer.position() == buffer.capacity()) {
adjustBuffer(2 * buffer.capacity());
}

int readbytes = 0;
int ret = 0;
while (true) {
ret = read0();

System.out.println("buffer=" + buffer);

if (ret > 0) {
readbytes += ret;
if (buffer.position() == buffer.capacity()) {
adjustBuffer(2 * buffer.capacity());
System.out.println(Thread.currentThread().getName()
+ " session " + this + " channel " + channel
+ " buffer=" + buffer + " buffer is full " + " "
+ System.nanoTime());
}
} else if (ret == 0) {
break;
} else {
throw new EOFException("peer connection close.");
}
}
if (protocolHandler == null) {
protocolHandler = new DefaultProtocolHandler();
}
if (protocolHandler.isAllDataRecved(buffer)) {
final SocketSession s = this;
try {
Runnable r = new Runnable() {

public void run() {
filters.DataReceived(s);
}
};
System.out.println(channel.socket().getInetAddress());
//if(channel.socket().getInetAddress().isAnyLocalAddress()){
Socket s1 = channel.socket();
if (s1.getInetAddress().isLoopbackAddress()) {
System.out.println("---is local----");
new Thread(r).start();
} else {
ExcuterManager.parserExe.execute(r);
}
} catch (Throwable e) {
this.close();
}
} else {
continueRead();
}
}

public SocketChannel getChannel() {
return channel;
}

public ByteBuffer getBuffer() {
return buffer;
}

public void continueRead() throws IOException {
channel.register(selector, SelectionKey.OP_READ, this);
}

public void adjustBuffer(int len) {
if (len > buffer.position()) {
ByteBuffer newBuffer = ByteBuffer.allocate(len);
buffer.flip();
newBuffer.put(buffer);
buffer = newBuffer;
}
}

// public void sendData(ByteBuffer buff) throws IOException{
// buffer.clear();
// continueRead();
// OutputWriter.flushChannel(channel, buff);
// }

public ProtocolHandler getProtocolHandler() {
return protocolHandler;
}

public void setProtocolHandler(ProtocolHandler protocolHandler) {
this.protocolHandler = protocolHandler;
}

public void close() {
buffer = null;
filters = null;
try {
channel.close();
} catch (Throwable e) {
}
channel = null;
selector = null;
protocolHandler = null;
// attributes = null;
}

public void setSslc(SSLContext sslc) {
this.sslEngine = sslc.createSSLEngine();

System.out.println("setsslc,sslengine=" + sslEngine);

sslEngine.setUseClientMode(false);
sslEngine.setNeedClientAuth(false);
sslEngine.setWantClientAuth(false);

// SSLSession session1 = sslEngine.getSession();
//ByteBuffer myAppData = ByteBuffer.allocate(sslEngine.getSession().getApplicationBufferSize());
ByteBuffer myNetData = ByteBuffer.allocate(sslEngine.getSession().getPacketBufferSize());
ByteBuffer peerNetData = ByteBuffer.allocate(sslEngine.getSession().getPacketBufferSize());



try {
doHandshake(channel,sslEngine,myNetData,peerNetData);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HandshakeStatus hs = sslEngine.getHandshakeStatus();
System.out.println("in setsslcontext,shakehand status=" + hs);

//writingEncryptedData = false;

}

public SSLEngine getSslEngine() {
return sslEngine;
}

public int read0() throws IOException {

int dd = channel.read(inNetBuffer);

SSLEngineResult res = sslEngine.unwrap(inNetBuffer, buffer);
System.out.println("in read0,status=" + res);

return dd;
}

public void sendData(ByteBuffer buff) throws IOException {
//sslEngine.wrap(buff, outNetBuffer);
OutputWriter.flushChannel(channel, outNetBuffer);
}

void doHandshake(SocketChannel socketChannel, SSLEngine engine,
ByteBuffer myNetData, ByteBuffer peerNetData) throws Exception {


int appBufferSize = engine.getSession().getApplicationBufferSize();
ByteBuffer myAppData = ByteBuffer.allocate(appBufferSize);
ByteBuffer peerAppData = ByteBuffer.allocate(appBufferSize);

// Begin handshake
engine.beginHandshake();
SSLEngineResult.HandshakeStatus hs = engine.getHandshakeStatus();

// Process handshaking message
while ((hs = engine.getHandshakeStatus()) != SSLEngineResult.HandshakeStatus.FINISHED &&
(hs = engine.getHandshakeStatus()) != SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING) {

switch (hs) {

case NEED_UNWRAP:
// Receive handshaking data from peer
if (socketChannel.read(peerNetData) < 0) {
// Handle closed channel
}

// Process incoming handshaking data
peerNetData.flip();
SSLEngineResult res = engine.unwrap(peerNetData, peerAppData);
peerNetData.compact();
hs = res.getHandshakeStatus();


// Check status
switch (res.getStatus()) {
case OK :
// if( hs == SSLEngineResult.HandshakeStatus.FINISHED ){
// break myio;
// }
break;
case BUFFER_OVERFLOW:
System.out.println("---------overflow-------");
break;
case BUFFER_UNDERFLOW:
System.out.println("---------underflow-------");
break;

// Handle other status: BUFFER_UNDERFLOW, BUFFER_OVERFLOW, CLOSED
//...
}
break;

case NEED_WRAP :
// Empty the local network packet buffer.
myNetData.clear();

// Generate handshaking data
res = engine.wrap(myAppData, myNetData);
hs = res.getHandshakeStatus();
//HandshakeStatus hs2 = engine.getHandshakeStatus();
// Check status
switch (res.getStatus()) {
case OK :
// if( hs == SSLEngineResult.HandshakeStatus.FINISHED ){
// break myio;
// }
myNetData.flip();

// Send the handshaking data to peer
while (myNetData.hasRemaining()) {
if (socketChannel.write(myNetData) < 0) {
// Handle closed channel
}
}

break;

// Handle other status: BUFFER_OVERFLOW, BUFFER_UNDERFLOW, CLOSED
//...
}
break;

case NEED_TASK :
new Thread( sslEngine.getDelegatedTask() ).start();
// Handle blocking tasks
break;

// Handle other status: // FINISHED or NOT_HANDSHAKING
//...
}
}

// Processes after handshaking
//...
}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 6 2008
Added on Aug 21 2008
2 comments
260 views