Author: elecharny
Date: Sat Dec 10 18:36:43 2011
New Revision: 1212840

URL: http://svn.apache.org/viewvc?rev=1212840&view=rev
Log:
Fixed the SSL handshake : we were destroying the FINISHING state by reading the 
status twice. The SSL handshake now works, and just need some cleanup and 
improvement.

Modified:
    mina/trunk/core/src/main/java/org/apache/mina/session/SslHelper.java
    
mina/trunk/core/src/main/java/org/apache/mina/transport/tcp/NioSelectorProcessor.java

Modified: mina/trunk/core/src/main/java/org/apache/mina/session/SslHelper.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/session/SslHelper.java?rev=1212840&r1=1212839&r2=1212840&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/session/SslHelper.java 
(original)
+++ mina/trunk/core/src/main/java/org/apache/mina/session/SslHelper.java Sat 
Dec 10 18:36:43 2011
@@ -257,13 +257,11 @@ public class SslHelper
 
             switch (result.getStatus()) {
                 case OK :
-                    System.out.println( "------------------> OK" );
                     accBuffer = null;
                     
                     return Status.OK;
                     
                 case BUFFER_UNDERFLOW :
-                    System.out.println( "------------------> UNDERFLOW" );
                     // We need to read some more data from the channel.
                     if (this.accBuffer == null) {
                         this.accBuffer = 
ByteBuffer.allocate(tempBuffer.capacity() + 4096);
@@ -275,13 +273,11 @@ public class SslHelper
                     return Status.BUFFER_UNDERFLOW;
     
                 case CLOSED :
-                    System.out.println( "------------------> CLOSED" );
                     // Get out
                     accBuffer = null;
                     throw new IllegalStateException();
     
                 case BUFFER_OVERFLOW :
-                    System.out.println( "------------------> OVERFLOW" );
                     // We have to increase the appBuffer size. In any case
                     // we aren't processing an handshake here. Read again.
                     appBuffer = ByteBuffer.allocate(appBuffer.capacity() + 
4096 );

Modified: 
mina/trunk/core/src/main/java/org/apache/mina/transport/tcp/NioSelectorProcessor.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/transport/tcp/NioSelectorProcessor.java?rev=1212840&r1=1212839&r2=1212840&view=diff
==============================================================================
--- 
mina/trunk/core/src/main/java/org/apache/mina/transport/tcp/NioSelectorProcessor.java
 (original)
+++ 
mina/trunk/core/src/main/java/org/apache/mina/transport/tcp/NioSelectorProcessor.java
 Sat Dec 10 18:36:43 2011
@@ -288,6 +288,8 @@ public class NioSelectorProcessor implem
 
         // map for finding read keys associated with a given session
         private Map<NioTcpSession, SelectionKey> sessionReadKey = new 
HashMap<NioTcpSession, SelectionKey>();
+        
+        private boolean handshaking = false;
 
         @Override
         public void run() {
@@ -456,7 +458,6 @@ public class NioSelectorProcessor implem
             SocketChannel channel = session.getSocketChannel();
             readBuffer.clear();
             int readCount = channel.read(readBuffer);
-            System.out.println( "------------------> read " + readCount + 
"bytes" );
 
             LOGGER.debug("read {} bytes", readCount);
 
@@ -489,8 +490,12 @@ public class NioSelectorProcessor implem
             HandshakeStatus hsStatus = engine.getHandshakeStatus();
             boolean processingData = true;
             
-            // Start the Handshake
-            engine.beginHandshake();
+            // Start the Handshake if we aren't already processing a HandShake
+            if (!handshaking) {
+                engine.beginHandshake();
+                handshaking = true;
+            }
+            
             hsStatus = engine.getHandshakeStatus();
 
             // If the SSLEngine has not be started, then the status will be 
NOT_HANDSHAKING
@@ -499,22 +504,21 @@ public class NioSelectorProcessor implem
                    processingData) {
                 switch (hsStatus) {
                     case NEED_TASK :
-                        System.out.println( "------------------> NEED_TASK" );
                         hsStatus = sslHelper.processTasks(engine);
                         
                         break;
                         
                     case NEED_WRAP :
-                        System.out.println( "------------------> NEED_WRAP" );
                         if ( LOGGER.isDebugEnabled()) {
                             LOGGER.debug("{} processing the NEED_WRAP state", 
session);
                         }
                         
                         int capacity = 
engine.getSession().getPacketBufferSize();
                         ByteBuffer outBuffer = ByteBuffer.allocate(capacity);
+                        SSLEngineResult result = null;
 
                         while (true) {
-                            SSLEngineResult result = engine.wrap(EMPTY_BUFFER, 
outBuffer);
+                            result = engine.wrap(EMPTY_BUFFER, outBuffer);
                             
                             if (result.getStatus() == 
SSLEngineResult.Status.BUFFER_OVERFLOW) {
                                 // TODO : increase the AppBuffer size
@@ -525,13 +529,15 @@ public class NioSelectorProcessor implem
         
                         outBuffer.flip();
                         session.write(outBuffer);
-                        processingData = false;
-                        hsStatus = engine.getHandshakeStatus();
+                        hsStatus = result.getHandshakeStatus();
+
+                        // We continue to loop while we don't expect messages 
to unwrap,
+                        // otherwise, we have to exit the loop.
+                        processingData = (hsStatus != 
HandshakeStatus.NEED_UNWRAP);
 
                         break;
                         
                     case NEED_UNWRAP :
-                        System.out.println( "------------------> NEED_UNWRAP" 
);
                         Status status = sslHelper.processUnwrap(engine, 
inBuffer, EMPTY_BUFFER);
 
                         if ( status == Status.BUFFER_UNDERFLOW) {
@@ -545,15 +551,13 @@ public class NioSelectorProcessor implem
                 }
             }
             
-            System.out.println( "======================> " + hsStatus);
-            
             if (hsStatus == HandshakeStatus.FINISHED) {
-                System.out.println( "------------------> FINISHED" );
                 if ( LOGGER.isDebugEnabled()) {
                     LOGGER.debug("{} processing the FINISHED state", session);
                 }
                 
                 session.changeState(SessionState.SECURED);
+                handshaking = false;
 
                 return true;
             }
@@ -590,8 +594,6 @@ public class NioSelectorProcessor implem
 
                     int wrote = session.getSocketChannel().write(buf);
 
-                    System.out.println( "------------------> Written" + 
buf.limit() + " bytes");
-
                     if (LOGGER.isDebugEnabled()) {
                         LOGGER.debug("wrote {} bytes to {}", wrote, session);
                     }


Reply via email to