harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From odea...@apache.org
Subject svn commit: r997362 - 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 native/jsse/shared/sslEngine.h
Date Wed, 15 Sep 2010 15:18:40 GMT
Author: odeakin
Date: Wed Sep 15 15:18:37 2010
New Revision: 997362

URL: http://svn.apache.org/viewvc?rev=997362&view=rev
Log:
Apply x-net-7.patch patch for HARMONY-6627 ([classlib][x-net] Creating a JSSE provider based
on OpenSSL) - Update to wrap() and unwrap() methods allowing handshake to complete.

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
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslEngine.h

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=997362&r1=997361&r2=997362&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
Wed Sep 15 15:18:37 2010
@@ -32,6 +32,8 @@ import javax.net.ssl.SSLEngineResult;
 import javax.net.ssl.SSLException;
 import javax.net.ssl.SSLSession;
 
+import org.apache.harmony.nio.AddressUtil;
+
 /**
  * Implementation of SSLEngine.
  * @see javax.net.ssl.SSLEngine class documentation for more information.
@@ -80,12 +82,12 @@ public class SSLEngineImpl extends SSLEn
     private static native void initImpl();
     private static native long initSSL(long context);
     private static native long initSSLEngine(long context);
-    private static native SSLEngineResult.HandshakeStatus connectImpl(long sslEngineAddress);
-    private static native SSLEngineResult.HandshakeStatus acceptImpl(long sslEngineAddress);
+    private static native SSLEngineResult.HandshakeStatus connectImpl(long ssl);
+    private static native SSLEngineResult.HandshakeStatus acceptImpl(long ssl);
     private static native SSLEngineResult wrapImpl(long sslEngineAddress,
-            byte[] src, int src_len, byte[] dst, int dst_len);
+            long src_address, int src_len, long dst_address, int dst_len);
     private static native SSLEngineResult unwrapImpl(long sslEngineAddress,
-            byte[] src, int src_len, byte[] dst, int dst_len);
+            long src_address, int src_len, long dst_address, int dst_len);
     
     /**
      * Ctor
@@ -134,12 +136,12 @@ public class SSLEngineImpl extends SSLEn
                     logger.println("SSLEngineImpl: CLIENT connecting");
                 }
 
-                handshakeStatus = connectImpl(SSLEngineAddress);
+                handshakeStatus = connectImpl(SSL);
             } else {
                 if (logger != null) {
                     logger.println("SSLEngineImpl: SERVER accepting connection");
                 }
-                handshakeStatus = acceptImpl(SSLEngineAddress);
+                handshakeStatus = acceptImpl(SSL);
             }
         }
     }
@@ -432,11 +434,46 @@ public class SSLEngineImpl extends SSLEn
         if (!handshake_started) {
             beginHandshake();
         }
-
+        
+        // get direct addresses to the buffers
         // only use the first buffer at the moment
-        byte[] dst = dsts[0].array();
-        int dst_len = dst.length;
-        return unwrapImpl(SSLEngineAddress, src.array(), src.array().length, dst, dst_len);
+        ByteBuffer src_temp_buffer = null, dst_temp_buffer = null;
+        long src_address, dst_address;
+        int src_length = src.remaining();
+        int dst_length = dsts[0].remaining();
+        if (src.isDirect()) {
+            src_address = AddressUtil.getDirectBufferAddress(src) + src.position();
+        } else {
+            // create a temporary buffer and copy the contents
+            src_temp_buffer = ByteBuffer.allocateDirect(src_length);
+            src_temp_buffer.put(src.array(), src.position(), src_length);
+            src_temp_buffer.rewind();
+            src_address = AddressUtil.getDirectBufferAddress(src_temp_buffer);
+        }
+        if (dsts[0].isDirect()) {
+            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);
+        }
+        
+        SSLEngineResult result = unwrapImpl(SSLEngineAddress, src_address, src_length, dst_address,
dst_length);
+        
+        // update the buffers contents and positions
+        src.position(src.position() + result.bytesConsumed());
+
+        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
+            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());
+        }
+        
+        return result;
     }
 
     /**
@@ -473,11 +510,45 @@ public class SSLEngineImpl extends SSLEn
             beginHandshake();
         }
         
+        // get direct addresses to the buffers
         // only use the first buffer at the moment
-        byte[] src = srcs[0].array();
-        int src_len = src.length;
+        ByteBuffer src_temp_buffer = null, dst_temp_buffer = null;
+        long src_address, dst_address;
+        int src_length = srcs[0].remaining();
+        int dst_length = dst.remaining();
+        if (srcs[0].isDirect()) {
+            src_address = AddressUtil.getDirectBufferAddress(srcs[0]) + srcs[0].position();
+        } else {
+            // create a temporary buffer and copy the contents
+            src_temp_buffer = ByteBuffer.allocateDirect(src_length);
+            src_temp_buffer.put(srcs[0].array(), srcs[0].position(), src_length);
+            src_temp_buffer.rewind();
+            src_address = AddressUtil.getDirectBufferAddress(src_temp_buffer);
+        }
+        if (dst.isDirect()) {
+            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);
+        }
+        
+        SSLEngineResult result = wrapImpl(SSLEngineAddress, src_address, src_length, dst_address,
dst_length);
+        
+        // update the buffers contents and positions
+        srcs[0].position(srcs[0].position() + result.bytesConsumed());
+
+        if (dst_temp_buffer == null) {
+            dst.position(dst.position() + result.bytesConsumed());
+        } else {
+            // if reading and 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());
+        }
         
-        return wrapImpl(SSLEngineAddress, src, src_len, dst.array(), dst.array().length);
+        return result;
     }
     
     // Shutdownes the engine and makes all cleanup work.

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=997362&r1=997361&r2=997362&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
Wed Sep 15 15:18:37 2010
@@ -100,64 +100,36 @@ JNIEXPORT jlong JNICALL Java_org_apache_
 }
 
 JNIEXPORT jobject JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLEngineImpl_acceptImpl
-  (JNIEnv *env, jclass clazz, jlong jsslengine) {
-    _sslengine *sslengine = jlong2addr(_sslengine, jsslengine);
-    SSL *ssl = NULL;
-    BIO *bio = sslengine->bio;
-    BIO *server_io = sslengine->bio_io;
+  (JNIEnv *env, jclass clazz, jlong jssl) {
+    SSL *ssl = jlong2addr(SSL, jssl);
     int ret;
     
-    BIO_get_ssl(bio, &ssl);
     // Put our SSL into accept state
     SSL_set_accept_state(ssl);
     // Start the client handshake
     ret = SSL_do_handshake(ssl);
     
-    fprintf(stderr, "Server bio pending: %d\n", BIO_ctrl_pending(bio));
-    fprintf(stderr, "Server bio can write: %d\n", BIO_ctrl_get_write_guarantee(bio));
-    fprintf(stderr, "Server bio read request: %d\n", BIO_ctrl_get_read_request(bio));
-    fprintf(stderr, "Server IO pending: %d\n", BIO_ctrl_pending(server_io));
-    fprintf(stderr, "Server IO can write: %d\n", BIO_ctrl_get_write_guarantee(server_io));
-    fprintf(stderr, "Server IO read request: %d\n", BIO_ctrl_get_read_request(server_io));
     return handshake_need_unwrap;
     //return getHandshakeStatus(env, SSL_get_error(ssl, ret));
 }
 
 JNIEXPORT jobject JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLEngineImpl_connectImpl
-  (JNIEnv *env, jclass clazz, jlong jsslengine) {
-    _sslengine *sslengine = jlong2addr(_sslengine, jsslengine);
-    SSL *ssl = NULL;
-    BIO *bio = sslengine->bio;
-    BIO *client_io = sslengine->bio_io;
+  (JNIEnv *env, jclass clazz, jlong jssl) {
+    SSL *ssl = jlong2addr(SSL, jssl);
     int ret;
     
-    BIO_get_ssl(bio, &ssl);
-    // Put our SSL into connect state
+    // Put our SSL into accept state
     SSL_set_connect_state(ssl);
-
-    // Start the client handshake
-    fprintf(stderr, "before handshake: SSL in init? %d : %s\n", SSL_in_init(ssl), SSL_state_string_long(ssl));
-    fprintf(stderr, "Client bio pending: %d\n", BIO_ctrl_pending(bio));
-    fprintf(stderr, "Client bio can write: %d\n", BIO_ctrl_get_write_guarantee(bio));
-    fprintf(stderr, "Client bio read request: %d\n", BIO_ctrl_get_read_request(bio));
-    fprintf(stderr, "Client IO pending: %d\n", BIO_ctrl_pending(client_io));
-    fprintf(stderr, "Client IO can write: %d\n", BIO_ctrl_get_write_guarantee(client_io));
-    fprintf(stderr, "Client IO read request: %d\n", BIO_ctrl_get_read_request(client_io));
+    // Start the server handshake
     ret = SSL_do_handshake(ssl);
-    fprintf(stderr, "after handshake: SSL in init? %d : %s\n", SSL_in_init(ssl), SSL_state_string_long(ssl));
-    fprintf(stderr, "Client bio pending: %d\n", BIO_ctrl_pending(bio));
-    fprintf(stderr, "Client bio can write: %d\n", BIO_ctrl_get_write_guarantee(bio));
-    fprintf(stderr, "Client bio read request: %d\n", BIO_ctrl_get_read_request(bio));
-    fprintf(stderr, "Client IO pending: %d\n", BIO_ctrl_pending(client_io));
-    fprintf(stderr, "Client IO can write: %d\n", BIO_ctrl_get_write_guarantee(client_io));
-    fprintf(stderr, "Client IO read request: %d\n", BIO_ctrl_get_read_request(client_io));
+    
     return handshake_need_wrap;
     //return getHandshakeStatus(env, SSL_get_error(ssl, ret));
 }
 
 JNIEXPORT jobject JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLEngineImpl_wrapImpl
-  (JNIEnv *env, jclass clazz, jlong jsslengine, jbyteArray src, int src_len, 
-  jbyteArray dst, int dst_len) {
+  (JNIEnv *env, jclass clazz, jlong jsslengine, jlong src_address, int src_len, 
+  jlong dst_address, int dst_len) {
     _sslengine *sslengine = jlong2addr(_sslengine, jsslengine);
     BIO *bio = sslengine->bio;
     BIO *bio_io = sslengine->bio_io;
@@ -166,16 +138,15 @@ JNIEXPORT jobject JNICALL Java_org_apach
     jobject handshake_state = NULL, engine_state = NULL, result = NULL;
     jclass result_class;
     jmethodID result_constructor;
-    jbyte *buffer;
+    jbyte *src_buffer = jlong2addr(jbyte, src_address);
+    jbyte *dst_buffer = jlong2addr(jbyte, dst_address);
 
     BIO_get_ssl(bio, &ssl);
 
     fprintf(stderr, ">wrap 1: SSL in init? %d : %s\n", SSL_in_init(ssl), SSL_state_string_long(ssl));
     
     // write input data
-    buffer = (jbyte*) malloc(src_len * sizeof(jbyte*));
-    (*env)->GetByteArrayRegion(env, src, 0, src_len, buffer);
-    write_result = BIO_write(bio, (const void *)buffer, (int)src_len);
+    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
@@ -190,13 +161,11 @@ JNIEXPORT jobject JNICALL Java_org_apach
         handshake_state = handshake_need_unwrap;
         engine_state = engine_ok;
     }
-    free(buffer);
     
     fprintf(stderr, ">wrap 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_io, buffer, dst_len);
+    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));
@@ -206,15 +175,11 @@ JNIEXPORT jobject JNICALL Java_org_apach
     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) {
-      (*env)->SetByteArrayRegion(env, dst, 0, read_result, buffer);
-    }
+
     if (read_result < 0) {
         // change state?
         read_result = 0;
     }
-    free(buffer);
     
     // construct return object
     result_class = (*env)->FindClass(env, "javax/net/ssl/SSLEngineResult");
@@ -226,8 +191,8 @@ JNIEXPORT jobject JNICALL Java_org_apach
 }
 
 JNIEXPORT jobject JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLEngineImpl_unwrapImpl
-  (JNIEnv *env, jclass clazz, jlong jsslengine, jbyteArray src, int src_len, 
-  jbyteArray dst, int dst_len) {
+  (JNIEnv *env, jclass clazz, jlong jsslengine, jlong src_address, int src_len, 
+  jlong dst_address, int dst_len) {
     _sslengine *sslengine = jlong2addr(_sslengine, jsslengine);
     BIO *bio = sslengine->bio;
     BIO *bio_io = sslengine->bio_io;
@@ -236,29 +201,32 @@ JNIEXPORT jobject JNICALL Java_org_apach
     jobject handshake_state = NULL, engine_state = NULL, result = NULL;
     jclass result_class;
     jmethodID result_constructor;
-    jbyte *buffer;
+    jbyte *src_buffer = jlong2addr(jbyte, src_address);
+    jbyte *dst_buffer = jlong2addr(jbyte, dst_address);
 
     BIO_get_ssl(bio, &ssl);
 
     fprintf(stderr, ">unwrap 1: SSL in init? %d : %s\n", SSL_in_init(ssl), SSL_state_string_long(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);
+    //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);
+    //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);
+    //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) {
         // wrote some data so must not be handshaking
@@ -283,10 +251,10 @@ JNIEXPORT jobject JNICALL Java_org_apach
     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);
+    //if (read_result > 0) {
+    //  (*env)->SetByteArrayRegion(env, dst, 0, read_result, buffer);
+    //}
+    //free(buffer);
     
     // construct return object
     result_class = (*env)->FindClass(env, "javax/net/ssl/SSLEngineResult");

Modified: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslEngine.h
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslEngine.h?rev=997362&r1=997361&r2=997362&view=diff
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslEngine.h
(original)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslEngine.h
Wed Sep 15 15:18:37 2010
@@ -32,9 +32,9 @@ JNIEXPORT jobject JNICALL Java_org_apach
 JNIEXPORT jobject JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLEngineImpl_connectImpl
   (JNIEnv *, jclass, jlong);
 JNIEXPORT jobject JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLEngineImpl_wrapImpl
-  (JNIEnv *, jclass, jlong, jbyteArray, int, jbyteArray, int);
+  (JNIEnv *, jclass, jlong, jlong, int, jlong, int);
 JNIEXPORT jobject JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLEngineImpl_unwrapImpl
-  (JNIEnv *, jclass, jlong, jbyteArray, int, jbyteArray, int);
+  (JNIEnv *, jclass, jlong, jlong, int, jlong, int);
 JNIEXPORT void JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLEngineImpl_closeInboundImpl
   (JNIEnv *, jclass, jlong);
 JNIEXPORT void JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLEngineImpl_closeOutboundImpl



Mime
View raw message