harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From odea...@apache.org
Subject svn commit: r997649 - in /harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main: java/org/apache/harmony/xnet/provider/jsse/SSLEngineImpl.java native/jsse/shared/sslEngine.c
Date Thu, 16 Sep 2010 09:28:06 GMT
Author: odeakin
Date: Thu Sep 16 09:28:06 2010
New Revision: 997649

URL: http://svn.apache.org/viewvc?rev=997649&view=rev
Log:
Apply x-net-8.patch patch for HARMONY-6627 ([classlib][x-net] Creating a JSSE provider based
on OpenSSL) - update handshake status correctly.

Modified:
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLEngineImpl.java
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslEngine.c

Modified: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLEngineImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLEngineImpl.java?rev=997649&r1=997648&r2=997649&view=diff
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLEngineImpl.java
(original)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLEngineImpl.java
Thu Sep 16 09:28:06 2010
@@ -17,17 +17,10 @@
 
 package org.apache.harmony.xnet.provider.jsse;
 
-import java.io.FileDescriptor;
-import java.io.IOException;
-import java.lang.reflect.Field;
-import java.nio.BufferUnderflowException;
 import java.nio.ByteBuffer;
 import java.nio.ReadOnlyBufferException;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
 
 import javax.net.ssl.SSLEngine;
-import javax.net.ssl.SSLHandshakeException;
 import javax.net.ssl.SSLEngineResult;
 import javax.net.ssl.SSLException;
 import javax.net.ssl.SSLSession;
@@ -48,16 +41,12 @@ public class SSLEngineImpl extends SSLEn
     private boolean isInboundDone = false;
     // indicates if outbound operations finished
     private boolean isOutboundDone = false;
-    // indicates if close_notify alert had been sent to another peer
-    private boolean close_notify_was_sent = false;
-    // indicates if close_notify alert had been received from another peer
-    private boolean close_notify_was_received = false;
     // indicates if engine was closed (it means that
     // all the works on it are done, except (probably) some finalizing work)
     private boolean engine_was_closed = false;
-    // indicates if engine was shutted down (it means that
+    // indicates if engine was shut down (it means that
     // all cleaning work had been done and the engine is not operable)
-    private boolean engine_was_shutteddown = false;
+    private boolean engine_was_shutdown = false;
 
     // active session object
     private SSLSessionImpl session;
@@ -373,7 +362,7 @@ public class SSLEngineImpl extends SSLEn
      */
     @Override
     public SSLEngineResult.HandshakeStatus getHandshakeStatus() {
-        if (!handshake_started || engine_was_shutteddown) {
+        if (!handshake_started || engine_was_shutdown) {
             // initial handshake has not been started yet
             return SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING;
         }
@@ -427,7 +416,7 @@ public class SSLEngineImpl extends SSLEn
     @Override
     public SSLEngineResult unwrap(ByteBuffer src, ByteBuffer[] dsts,
                                 int offset, int length) throws SSLException {
-        if (engine_was_shutteddown) {
+        if (engine_was_shutdown) {
             return new SSLEngineResult(SSLEngineResult.Status.CLOSED,
                     SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING, 0, 0);
         }
@@ -459,7 +448,6 @@ public class SSLEngineImpl extends SSLEn
             dst_address = AddressUtil.getDirectBufferAddress(dsts[0]) + dsts[0].position();
         } else {
             dst_temp_buffer = ByteBuffer.allocateDirect(dst_length);
-            src_temp_buffer.rewind();
             dst_address = AddressUtil.getDirectBufferAddress(dst_temp_buffer);
         }
         
@@ -471,13 +459,15 @@ public class SSLEngineImpl extends SSLEn
         if (dst_temp_buffer == null) {
             dsts[0].position(dsts[0].position() + result.bytesConsumed());
         } else {
-            // if reading and a temporary buffer was used, copy buffer contents
+            // if a temporary buffer was used, copy buffer contents
             int position = dsts[0].position();
             dsts[0].put(dst_temp_buffer);
             // adjust position as not all bytes may have been written
             dsts[0].position(position + result.bytesProduced());
         }
         
+        // update handshake status
+        handshakeStatus = result.getHandshakeStatus();
         return result;
     }
 
@@ -499,7 +489,7 @@ public class SSLEngineImpl extends SSLEn
     @Override
     public SSLEngineResult wrap(ByteBuffer[] srcs, int offset,
                             int len, ByteBuffer dst) throws SSLException {
-        if (engine_was_shutteddown) {
+        if (engine_was_shutdown) {
             return new SSLEngineResult(SSLEngineResult.Status.CLOSED,
                     SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING, 0, 0);
         }
@@ -534,7 +524,6 @@ public class SSLEngineImpl extends SSLEn
             dst_address = AddressUtil.getDirectBufferAddress(dst) + dst.position();
         } else {
             dst_temp_buffer = ByteBuffer.allocateDirect(dst_length);
-            src_temp_buffer.rewind();
             dst_address = AddressUtil.getDirectBufferAddress(dst_temp_buffer);
         }
         
@@ -546,20 +535,22 @@ public class SSLEngineImpl extends SSLEn
         if (dst_temp_buffer == null) {
             dst.position(dst.position() + result.bytesConsumed());
         } else {
-            // if reading and a temporary buffer was used, copy buffer contents
+            // if a temporary buffer was used, copy buffer contents
             int position = dst.position();
             dst.put(dst_temp_buffer);
             // adjust position as not all bytes may have been written
             dst.position(position + result.bytesProduced());
         }
         
+        // update handshake status
+        handshakeStatus = result.getHandshakeStatus();
         return result;
     }
     
     // Shutdownes the engine and makes all cleanup work.
     private void shutdown() {
         engine_was_closed = true;
-        engine_was_shutteddown = true;
+        engine_was_shutdown = true;
         isOutboundDone = true;
         isInboundDone = true;
     }

Modified: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslEngine.c
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslEngine.c?rev=997649&r1=997648&r2=997649&view=diff
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslEngine.c
(original)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslEngine.c
Thu Sep 16 09:28:06 2010
@@ -32,20 +32,16 @@ typedef struct {
 static jobject handshake_need_wrap, handshake_need_unwrap, handshake_finished, handshake_not_handshaking;
 static jobject engine_buffer_overflow, engine_buffer_underflow, engine_closed, engine_ok;
 
-jobject getHandshakeStatus(JNIEnv *env, int state) {
+int check_ssl_error(JNIEnv *env, int state) {
     jclass exception;
     switch(state) {
-    case SSL_ERROR_NONE:
-      return handshake_not_handshaking;
-    case SSL_ERROR_WANT_READ:
-      return handshake_need_unwrap;
-    case SSL_ERROR_WANT_WRITE:
-      return handshake_need_wrap;
-    default:
+    case SSL_ERROR_SYSCALL: 
+    case SSL_ERROR_SSL:
       exception = (*env)->FindClass(env, "javax/net/ssl/SSLHandshakeException");
       (*env)->ThrowNew(env, exception, ERR_reason_error_string(ERR_get_error()));
+      return 1;
     }
-    return NULL;
+    return 0;
 }
 
 JNIEXPORT void JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLEngineImpl_initImpl
@@ -109,8 +105,11 @@ JNIEXPORT jobject JNICALL Java_org_apach
     // Start the client handshake
     ret = SSL_do_handshake(ssl);
     
+    if (check_ssl_error(env, SSL_get_error(ssl, ret))) {
+        return NULL;
+    }
+    
     return handshake_need_unwrap;
-    //return getHandshakeStatus(env, SSL_get_error(ssl, ret));
 }
 
 JNIEXPORT jobject JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLEngineImpl_connectImpl
@@ -123,8 +122,11 @@ JNIEXPORT jobject JNICALL Java_org_apach
     // Start the server handshake
     ret = SSL_do_handshake(ssl);
     
+    if (check_ssl_error(env, SSL_get_error(ssl, ret))) {
+        return NULL;
+    }
+    
     return handshake_need_wrap;
-    //return getHandshakeStatus(env, SSL_get_error(ssl, ret));
 }
 
 JNIEXPORT jobject JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLEngineImpl_wrapImpl
@@ -140,14 +142,13 @@ JNIEXPORT jobject JNICALL Java_org_apach
     jmethodID result_constructor;
     jbyte *src_buffer = jlong2addr(jbyte, src_address);
     jbyte *dst_buffer = jlong2addr(jbyte, dst_address);
+    int initial_init_state, init_state;
 
     BIO_get_ssl(bio, &ssl);
-
-    fprintf(stderr, ">wrap 1: SSL in init? %d : %s\n", SSL_in_init(ssl), SSL_state_string_long(ssl));
+    initial_init_state = SSL_in_init(ssl);
     
     // write input data
     write_result = BIO_write(bio, (const void *)src_buffer, (int)src_len);
-    fprintf(stderr, ">wrap BIO_write, result:%d \n", write_result);
     if (write_result > 0) {
         // wrote some data so must not be handshaking
         handshake_state = handshake_not_handshaking;
@@ -162,25 +163,26 @@ JNIEXPORT jobject JNICALL Java_org_apach
         engine_state = engine_ok;
     }
     
-    fprintf(stderr, ">wrap 2: SSL in init? %d : %s\n", SSL_in_init(ssl), SSL_state_string_long(ssl));
-    
     // read output data
     read_result = BIO_read(bio_io, dst_buffer, dst_len);
-    
-    fprintf(stderr, ">wrap read result: %d\n", read_result);
-    fprintf(stderr, ">wrap 3: SSL in init? %d : %s\n", SSL_in_init(ssl), SSL_state_string_long(ssl));
-    fprintf(stderr, ">wrap bio pending: %d\n", BIO_ctrl_pending(bio));
-    fprintf(stderr, ">wrap bio can write: %d\n", BIO_ctrl_get_write_guarantee(bio));
-    fprintf(stderr, ">wrap bio read request: %d\n", BIO_ctrl_get_read_request(bio));
-    fprintf(stderr, ">wrap IO pending: %d\n", BIO_ctrl_pending(bio_io));
-    fprintf(stderr, ">wrap IO can write: %d\n", BIO_ctrl_get_write_guarantee(bio_io));
-    fprintf(stderr, ">wrap IO read request: %d\n", BIO_ctrl_get_read_request(bio_io));
 
     if (read_result < 0) {
         // change state?
         read_result = 0;
     }
     
+    init_state = SSL_in_init(ssl);
+    
+    // if not in SSL init state
+    if (!init_state) {
+        // if we were in init state when we entered this function
+        if (initial_init_state) {
+            handshake_state = handshake_finished;
+        } else {
+            handshake_state = handshake_not_handshaking;
+        }
+    }
+    
     // construct return object
     result_class = (*env)->FindClass(env, "javax/net/ssl/SSLEngineResult");
     result_constructor = (*env)->GetMethodID(env, result_class, "<init>", 
@@ -203,29 +205,19 @@ JNIEXPORT jobject JNICALL Java_org_apach
     jmethodID result_constructor;
     jbyte *src_buffer = jlong2addr(jbyte, src_address);
     jbyte *dst_buffer = jlong2addr(jbyte, dst_address);
+    int initial_init_state, init_state;
 
     BIO_get_ssl(bio, &ssl);
-
-    fprintf(stderr, ">unwrap 1: SSL in init? %d : %s\n", SSL_in_init(ssl), SSL_state_string_long(ssl));
+    initial_init_state = SSL_in_init(ssl);
     
     // write input data
-    //buffer = (jbyte*) malloc(src_len * sizeof(jbyte*));
-    //(*env)->GetByteArrayRegion(env, src, 0, src_len, buffer);
-    //write_result = BIO_write(bio_io, (const void *)buffer, (int)src_len);
     write_result = BIO_write(bio_io, (const void *)src_buffer, (int)src_len);
-    fprintf(stderr, ">unwrap BIO_write, result:%d \n", write_result);
     if (write_result < 0) {
         // change state?
         write_result = 0;
     }
     
-    //free(buffer);
-    
-    fprintf(stderr, ">unwrap 2: SSL in init? %d : %s\n", SSL_in_init(ssl), SSL_state_string_long(ssl));
-    
     // read output data
-    //buffer = (jbyte*) malloc(dst_len * sizeof(jbyte*));
-    //read_result = BIO_read(bio, buffer, dst_len);
     read_result = BIO_read(bio, dst_buffer, dst_len);
     
     if (read_result > 0) {
@@ -242,19 +234,16 @@ JNIEXPORT jobject JNICALL Java_org_apach
         engine_state = engine_ok;
     }
     
-    fprintf(stderr, ">unwrap read result: %d\n", read_result);
-    fprintf(stderr, ">unwrap 3: SSL in init? %d : %s\n", SSL_in_init(ssl), SSL_state_string_long(ssl));
-    fprintf(stderr, ">unwrap bio pending: %d\n", BIO_ctrl_pending(bio));
-    fprintf(stderr, ">unwrap bio can write: %d\n", BIO_ctrl_get_write_guarantee(bio));
-    fprintf(stderr, ">unwrap bio read request: %d\n", BIO_ctrl_get_read_request(bio));
-    fprintf(stderr, ">unwrap IO pending: %d\n", BIO_ctrl_pending(bio_io));
-    fprintf(stderr, ">unwrap IO can write: %d\n", BIO_ctrl_get_write_guarantee(bio_io));
-    fprintf(stderr, ">unwrap IO read request: %d\n", BIO_ctrl_get_read_request(bio_io));
-    
-    //if (read_result > 0) {
-    //  (*env)->SetByteArrayRegion(env, dst, 0, read_result, buffer);
-    //}
-    //free(buffer);
+    init_state = SSL_in_init(ssl);
+    // if not in SSL init state
+    if (!init_state) {
+        // if we were in init state when we entered this function
+        if (initial_init_state) {
+            handshake_state = handshake_finished;
+        } else {
+            handshake_state = handshake_not_handshaking;
+        }
+    }
     
     // construct return object
     result_class = (*env)->FindClass(env, "javax/net/ssl/SSLEngineResult");



Mime
View raw message