harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From odea...@apache.org
Subject svn commit: r981527 - in /harmony/enhanced/java/branches/omd/classlib/modules: luni/src/main/java/java/net/ x-net/ x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/ x-net/src/main/native/ x-net/src/main/native/jsse/ x-net/src/main/native/jsse/...
Date Mon, 02 Aug 2010 14:41:53 GMT
Author: odeakin
Date: Mon Aug  2 14:41:52 2010
New Revision: 981527

URL: http://svn.apache.org/viewvc?rev=981527&view=rev
Log:
Make some initial changes to use OpenSSL underneath the x-net APIs. The native build for x-net
only has makefiles for Windows at the moment and assumes that the OpenSSL source is unpacked
and built in an "openssl" directory peer to the jsse native directory.

Added:
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/jsse_copyright.c
  (with props)
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslParameters.c
  (with props)
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslParameters.h
  (with props)
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslSocket.c
  (with props)
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslSocket.h
  (with props)
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/unix/
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/windows/
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/windows/hyjsse.def
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/windows/hyjsse.rc
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/windows/makefile
  (with props)
Modified:
    harmony/enhanced/java/branches/omd/classlib/modules/luni/src/main/java/java/net/Socket.java
    harmony/enhanced/java/branches/omd/classlib/modules/luni/src/main/java/java/net/SocketImpl.java
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/build.xml
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLParameters.java
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSocketImpl.java
    harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSocketInputStream.java

Modified: harmony/enhanced/java/branches/omd/classlib/modules/luni/src/main/java/java/net/Socket.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/omd/classlib/modules/luni/src/main/java/java/net/Socket.java?rev=981527&r1=981526&r2=981527&view=diff
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/luni/src/main/java/java/net/Socket.java
(original)
+++ harmony/enhanced/java/branches/omd/classlib/modules/luni/src/main/java/java/net/Socket.java
Mon Aug  2 14:41:52 2010
@@ -34,7 +34,7 @@ import org.apache.harmony.luni.util.Priv
  */
 public class Socket {
 
-    SocketImpl impl;
+    protected SocketImpl impl;
 
     static SocketImplFactory factory;
 

Modified: harmony/enhanced/java/branches/omd/classlib/modules/luni/src/main/java/java/net/SocketImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/omd/classlib/modules/luni/src/main/java/java/net/SocketImpl.java?rev=981527&r1=981526&r2=981527&view=diff
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/luni/src/main/java/java/net/SocketImpl.java
(original)
+++ harmony/enhanced/java/branches/omd/classlib/modules/luni/src/main/java/java/net/SocketImpl.java
Mon Aug  2 14:41:52 2010
@@ -159,7 +159,7 @@ public abstract class SocketImpl impleme
      * 
      * @return the file descriptor of this socket.
      */
-    protected FileDescriptor getFileDescriptor() {
+    public FileDescriptor getFileDescriptor() {
         return fd;
     }
 

Modified: harmony/enhanced/java/branches/omd/classlib/modules/x-net/build.xml
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/omd/classlib/modules/x-net/build.xml?rev=981527&r1=981526&r2=981527&view=diff
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/build.xml (original)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/build.xml Mon Aug  2 14:41:52
2010
@@ -27,7 +27,7 @@
     <property file="../../make/depends.properties" />
 
     <target name="build" depends="-build-no-check"/>
-    <target name="-build-no-check" depends="-build-jar,-test-jar" />
+    <target name="-build-no-check" depends="-build-jar,-build-native,-test-jar" />
 
     <target name="test" depends="-test-module">
         <fail message="Some tests failed">
@@ -40,6 +40,12 @@
         </fail>
     </target>
 
+    <!-- Build native code -->
+    <target name="-build-native" unless="skip.native.build" >
+        <!-- Build jsse dll -->
+        <make dir="src/main/native/jsse/${hy.os.family}" />
+    </target>
+
     <!-- internal target for local and global test run sequence -->
     <target name="-test-module" depends="build,-run-tests" />
 

Modified: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLParameters.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/SSLParameters.java?rev=981527&r1=981526&r2=981527&view=diff
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLParameters.java
(original)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLParameters.java
Mon Aug  2 14:41:52 2010
@@ -23,8 +23,11 @@ import java.security.KeyManagementExcept
 import java.security.KeyStore;
 import java.security.KeyStoreException;
 import java.security.NoSuchAlgorithmException;
+import java.security.PrivateKey;
 import java.security.SecureRandom;
 import java.security.UnrecoverableKeyException;
+import java.security.cert.CertificateEncodingException;
+import java.security.cert.X509Certificate;
 import javax.net.ssl.KeyManager;
 import javax.net.ssl.KeyManagerFactory;
 import javax.net.ssl.TrustManager;
@@ -81,6 +84,15 @@ public class SSLParameters {
     // if the peer with this parameters allowed to cteate new SSL session
     private boolean enable_session_creation = true;
 
+    // Native address of the OpenSSL SSL_CTX struct
+    private static long SSL_CTX = 0;
+
+    static {
+        System.loadLibrary("hyjsse");
+    }
+
+    private static native long initialiseContext(byte[][] trustCerts, byte[] keyCert, byte[]
privateKey);
+
     /**
      * Creates an instance of SSLParameters.
      */
@@ -113,7 +125,7 @@ public class SSLParameters {
                 if (defaultKeyManager == null) {
                     KeyManagerFactory kmf = KeyManagerFactory.getInstance(
                             KeyManagerFactory.getDefaultAlgorithm());
-                    kmf.init(null, null);            	
+                    kmf.init(null, null);
                     kms = kmf.getKeyManagers();
                     // tell that we are trying to initialize defaultKeyManager
                     initialize_default = true;
@@ -170,7 +182,7 @@ public class SSLParameters {
         } catch (KeyStoreException e) {
             throw new KeyManagementException(e);
         } catch (UnrecoverableKeyException e) {
-            throw new KeyManagementException(e);        	
+            throw new KeyManagementException(e);
         }
         // initialize secure random
         if (sr == null) {
@@ -181,6 +193,43 @@ public class SSLParameters {
         } else {
             secureRandom = sr;
         }
+
+        // Now setup our OpenSSL SSL_CTX with the various certificates
+        // First iterate through the trust certs storing their ASN1 form
+        X509Certificate[] acceptedIssuers = trustManager.getAcceptedIssuers();
+        int size = acceptedIssuers.length;
+        byte[][] tempCertsDER = new byte[size][];
+        for (int i=0; i<size; i++) {
+            try {
+                tempCertsDER[i] = acceptedIssuers[i].getEncoded();
+            } catch (CertificateEncodingException e) {
+                //TODO how to handle exceptions?
+                System.out.println("threw exception");
+            }
+        }
+
+        String alias = keyManager.chooseClientAlias(new String[] {"RSA", "DSA"}, null, null);
+        byte[] keyCertDER = null;
+        byte[] privateKeyDER = null;
+
+        if (alias != null) {
+            X509Certificate[] certs = keyManager.getCertificateChain(alias);
+            if (certs.length != 0) {
+                try {
+                    keyCertDER = certs[0].getEncoded();
+                } catch (CertificateEncodingException e) {
+                    //TODO how to handle exceptions?
+                    System.out.println("threw exception");
+                }
+            }
+    
+            PrivateKey privateKey = keyManager.getPrivateKey(alias);
+            if (privateKey != null) {
+                 privateKeyDER = keyManager.getPrivateKey(alias).getEncoded();
+            }
+        }
+
+        SSL_CTX = initialiseContext(tempCertsDER, keyCertDER, privateKeyDER);
     }
 
     protected static SSLParameters getDefault() throws KeyManagementException {
@@ -351,6 +400,10 @@ public class SSLParameters {
         return enable_session_creation;
     }
 
+    protected long getSSLContextAddress() {
+        return SSL_CTX;
+    }
+
     /**
      * Returns the clone of this object.
      * @return the clone.

Modified: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSocketImpl.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/SSLSocketImpl.java?rev=981527&r1=981526&r2=981527&view=diff
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSocketImpl.java
(original)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSocketImpl.java
Mon Aug  2 14:41:52 2010
@@ -22,6 +22,7 @@ import org.apache.harmony.xnet.provider.
 import org.apache.harmony.xnet.provider.jsse.SSLStreamedInput;
 import org.apache.harmony.xnet.provider.jsse.SSLSessionImpl;
 
+import java.io.FileDescriptor;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -78,6 +79,9 @@ public class SSLSocketImpl extends SSLSo
     // logger
     private Logger.Stream logger = Logger.getStream("socket");
 
+    // Pointer to the OpenSSL SSL struct used for this connection
+    private long SSL;
+
     // ----------------- Constructors and initializers --------------------
 
     /**
@@ -404,6 +408,9 @@ public class SSLSocketImpl extends SSLSo
         }
     }
 
+    private native long sslConnectImpl(long sslContextAddress, FileDescriptor fd);
+    private native long sslAcceptImpl(long sslContextAddress, FileDescriptor fd);
+
     /**
      * Performs the handshake process over the SSL/TLS connection
      * as described in rfc 2246, TLS v1 specification
@@ -428,27 +435,32 @@ public class SSLSocketImpl extends SSLSo
                 if (logger != null) {
                     logger.println("SSLSocketImpl: CLIENT");
                 }
-                handshakeProtocol = new ClientHandshakeImpl(this);
+
+                SSL = sslConnectImpl(sslParameters.getSSLContextAddress(), impl.getFileDescriptor());
+
+                //handshakeProtocol = new ClientHandshakeImpl(this);
             } else {
                 if (logger != null) {
                     logger.println("SSLSocketImpl: SERVER");
                 }
-                handshakeProtocol = new ServerHandshakeImpl(this);
+                SSL = sslAcceptImpl(sslParameters.getSSLContextAddress(), impl.getFileDescriptor());
+
+                //handshakeProtocol = new ServerHandshakeImpl(this);
             }
 
             alertProtocol = new AlertProtocol();
-            recordProtocol = new SSLRecordProtocol(handshakeProtocol,
+            /*recordProtocol = new SSLRecordProtocol(handshakeProtocol,
                     alertProtocol, new SSLStreamedInput(input),
-                    appDataIS.dataPoint);
+                    appDataIS.dataPoint);*/
         }
 
         if (logger != null) {
             logger.println("SSLSocketImpl.startHandshake");
         }
 
-        handshakeProtocol.start();
+        //handshakeProtocol.start();
 
-        doHandshake();
+        //doHandshake();
 
         if (logger != null) {
             logger.println("SSLSocketImpl.startHandshake: END");
@@ -591,13 +603,16 @@ public class SSLSocketImpl extends SSLSo
         socket_was_closed = true;
     }
 
+
+    private native byte needAppDataImpl(long ssl);
+
     /**
      * This method is called by SSLSocketInputStream class
      * when client application tryes to read application data from
      * the stream, but there is no data in its underlying buffer.
      * @throws  IOException
      */
-    protected void needAppData() throws IOException {
+    protected byte needAppData() throws IOException {
         if (!handshake_started) {
             startHandshake();
         }
@@ -605,7 +620,13 @@ public class SSLSocketImpl extends SSLSo
         if (logger != null) {
             logger.println("SSLSocket.needAppData..");
         }
-        try {
+
+        byte data = needAppDataImpl(SSL);
+        if (data == -1) {
+            appDataIS.setEnd();
+        }
+        return data;
+        /*try {
             while(appDataIS.available() == 0) {
                 // read and unwrap the record contained in the transport
                 // input stream (SSLStreamedInput), pass it
@@ -661,9 +682,11 @@ public class SSLSocketImpl extends SSLSo
         if (logger != null) {
             logger.println("SSLSocket.needAppData: app data len: "
                     + appDataIS.available());
-        }
+        }*/
     }
 
+
+    private native void writeAppDataImpl(long SSL, byte[] data, int offset, int len);
     /**
      * This method is called by SSLSocketOutputStream when client application
      * tryes to send the data over ssl protocol.
@@ -678,7 +701,9 @@ public class SSLSocketImpl extends SSLSo
                     len + " " + SSLRecordProtocol.MAX_DATA_LENGTH);
             //logger.println(new String(data, offset, len));
         }
-        try {
+
+        writeAppDataImpl(SSL, data, offset, len);
+        /*try {
             if (len < SSLRecordProtocol.MAX_DATA_LENGTH) {
                 output.write(recordProtocol.wrap(ContentType.APPLICATION_DATA,
                             data, offset, len));
@@ -699,7 +724,7 @@ public class SSLSocketImpl extends SSLSo
         } catch (AlertException e) {
             // will throw exception
             reportFatalAlert(e.getDescriptionCode(), e.getReason());
-        }
+        }*/
     }
 
     /*

Modified: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSocketInputStream.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/SSLSocketInputStream.java?rev=981527&r1=981526&r2=981527&view=diff
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSocketInputStream.java
(original)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSocketInputStream.java
Mon Aug  2 14:41:52 2010
@@ -113,9 +113,9 @@ public final class SSLSocketInputStream
             // If there is no data in the buffer
             // - will block untill the data will be provided by
             // record layer
-            owner.needAppData();
+            buffer[end++] = owner.needAppData();
         }
-        return buffer[pos++] & 0xFF;
+        return buffer[pos++];
     }
 
     /**

Added: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/jsse_copyright.c
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/jsse_copyright.c?rev=981527&view=auto
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/jsse_copyright.c
(added)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/jsse_copyright.c
Mon Aug  2 14:41:52 2010
@@ -0,0 +1,21 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+/* A copyright string included in each DLL and executable */
+
+const char hyCopyright[] =
+  "(c) Copyright 1991, 2010 The Apache Software Foundation or its licensors, as applicable.";

Propchange: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/jsse_copyright.c
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslParameters.c
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslParameters.c?rev=981527&view=auto
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslParameters.c
(added)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslParameters.c
Mon Aug  2 14:41:52 2010
@@ -0,0 +1,116 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+#include "sslParameters.h"
+#include <stdio.h>
+#include "jni.h"
+#include "hysock.h"
+#include "openssl/bio.h"
+#include "openssl/ssl.h"
+#include "openssl/err.h"
+
+JNIEXPORT jlong JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLParameters_initialiseContext
+  (JNIEnv *env, jclass clazz, jbyteArray jtrustCerts, jbyteArray jkeyCert, jbyteArray jprivateKey)
+{
+    SSL_CTX *context;
+    jint size;
+    jint i;
+    X509_STORE *certStore;
+    X509 *x509cert;
+    unsigned char *temp;
+    int ret;
+
+    SSL_library_init();
+    SSL_load_error_strings();
+    OpenSSL_add_all_algorithms();
+    context = SSL_CTX_new(SSLv23_method());
+
+    ret = SSL_CTX_set_cipher_list(context, "ALL");
+    if (ret<=0) {
+       ERR_print_errors_fp(stderr);
+    }
+
+    // First initilise the trust certificates in our newly created context
+    size = (*env)->GetArrayLength(env, jtrustCerts);
+    if (size) {
+        // Get the current trust certificate store and add any certs passed in
+        certStore = SSL_CTX_get_cert_store(context);
+        for (i=0; i<size; i++) {
+            jbyteArray cert = (jbyteArray) (*env)->GetObjectArrayElement(env, jtrustCerts,
i);
+            jint byteSize = (*env)->GetArrayLength(env, cert);
+            
+            // malloc a buffer for the ASN1 encoded certificate
+            jbyte *certBuffer = (jbyte*) malloc(byteSize * sizeof(jbyte*)); 
+            (*env)->GetByteArrayRegion(env, cert, 0, byteSize, certBuffer);
+
+            // Copy certBuffer as the d2i_X509 will increment it
+            temp = (unsigned char*)certBuffer;
+
+            // Create an X509 from the ASN1 encoded certificate
+            x509cert = d2i_X509(NULL, &temp, (int)byteSize);
+
+            // Add the certificate to the existing ones in the SSL_CTX
+            ret = X509_STORE_add_cert(certStore, x509cert);
+            if (ret<=0) {
+               ERR_print_errors_fp(stderr);
+            }
+            free(certBuffer);
+        }
+
+        // Carry out peer cert verification
+        // TODO: Is this the right setting?
+        SSL_CTX_set_verify(context, SSL_VERIFY_PEER, NULL);
+        SSL_CTX_set_verify_depth(context, 1);
+    }
+
+    if (jkeyCert != NULL) {
+        jint byteSize = (*env)->GetArrayLength(env, jkeyCert);
+            
+        // malloc a buffer for the ASN1 encoded certificate
+        jbyte *certBuffer = (jbyte*) malloc(byteSize * sizeof(jbyte*)); 
+        (*env)->GetByteArrayRegion(env, jkeyCert, 0, byteSize, certBuffer);
+
+        // Set the key cert passed in as the default for this context
+        ret = SSL_CTX_use_certificate_ASN1(context, byteSize, (unsigned char *)certBuffer);
+        if (ret<=0) {
+           ERR_print_errors_fp(stderr);
+        }
+        free(certBuffer);
+    }
+
+    if (jprivateKey != NULL) {
+        jint byteSize = (*env)->GetArrayLength(env, jprivateKey);
+            
+        // malloc a buffer for the ASN1 encoded certificate
+        jbyte *certBuffer = (jbyte*) malloc(byteSize * sizeof(jbyte*)); 
+        (*env)->GetByteArrayRegion(env, jprivateKey, 0, byteSize, certBuffer);
+
+        // Set the private key passed in as the default for this context
+        ret = SSL_CTX_use_PrivateKey_ASN1(EVP_PKEY_RSA, context, (unsigned char *)certBuffer,
byteSize);
+        if (ret<=0) {
+           ERR_print_errors_fp(stderr);
+        }
+
+        SSL_CTX_check_private_key(context);
+        if (ret<=0) {
+           ERR_print_errors_fp(stderr);
+        }
+        free(certBuffer);
+    }
+    
+    return (jlong)context;
+}

Propchange: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslParameters.c
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslParameters.h
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslParameters.h?rev=981527&view=auto
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslParameters.h
(added)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslParameters.h
Mon Aug  2 14:41:52 2010
@@ -0,0 +1,36 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+#include <jni.h>
+
+#ifndef _CONTEXT_H
+#define _CONTEXT_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+JNIEXPORT jlong JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLParameters_initialiseContext
+  (JNIEnv *, jclass, jobjectArray, jbyteArray, jbyteArray);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+

Propchange: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslParameters.h
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslSocket.c
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslSocket.c?rev=981527&view=auto
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslSocket.c
(added)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslSocket.c
Mon Aug  2 14:41:52 2010
@@ -0,0 +1,151 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+#include "sslSocket.h"
+#include <stdio.h>
+#include "jni.h"
+#include "hysock.h"
+#include "openssl/bio.h"
+#include "openssl/ssl.h"
+#include "openssl/err.h"
+#include "errno.h"
+
+jlong getFD(JNIEnv * env, jobject fd) {
+    jclass descriptorCLS;
+    jfieldID descriptorFID;
+    hysocket_t hysocketP;
+
+    //TODO add to cache
+    descriptorCLS = (*env)->FindClass (env, "java/io/FileDescriptor");
+    if (NULL == descriptorCLS){
+        return 0;
+    }
+
+    descriptorFID = (*env)->GetFieldID (env, descriptorCLS, "descriptor", "J");
+    if (NULL == descriptorFID){
+        return 0;
+    }
+
+    hysocketP = (hysocket_t) ((IDATA)((*env)->GetLongField (env, fd, descriptorFID)));
+    if (NULL == hysocketP) {
+        return 0;
+    }
+
+#if defined(WIN32) || defined(WIN64)
+    if (hysocketP->flags & SOCKET_IPV4_OPEN_MASK) {
+        return (jlong)(hysocketP->ipv4);
+    } else if (hysocketP->flags & SOCKET_IPV6_OPEN_MASK) {
+        return (jlong)(hysocketP->ipv6);
+    } else {
+        return 0;
+    }
+#else 
+    return (jlong)(hysocketP->sock);
+#endif
+}
+
+
+JNIEXPORT jlong JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLSocketImpl_sslAcceptImpl
+  (JNIEnv *env, jclass clazz, jlong context, jobject fd) {
+    jlong socket = getFD(env, fd);
+    SSL *ssl;
+    BIO *bio;
+    int ret;
+    SSL_CTX *ctx = (SSL_CTX*)context;
+ 
+    ssl = SSL_new(ctx);
+    bio = BIO_new_socket((int)socket, BIO_NOCLOSE);
+    SSL_set_bio(ssl, bio, bio);
+
+    SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
+
+    // Put our SSL into accept state
+    SSL_set_accept_state(ssl);
+
+    // Start the server handshake
+    ret = SSL_do_handshake(ssl);
+    if (ret<=0) {
+        jclass exception = (*env)->FindClass(env, "javax/net/ssl/SSLHandshakeException");
+        (*env)->ThrowNew(env, exception, ERR_reason_error_string(ERR_get_error())); 
+    }
+
+    return (jlong)ssl;
+}
+
+JNIEXPORT jlong JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLSocketImpl_sslConnectImpl
+  (JNIEnv *env, jclass clazz, jlong context, jobject fd) {
+    jlong socket = getFD(env, fd);
+    SSL *ssl;
+    BIO *bio;
+    int ret;
+    SSL_CTX *ctx = (SSL_CTX*)context;
+
+    ssl = SSL_new(ctx);
+    bio = BIO_new_socket((int)socket, BIO_NOCLOSE);
+    SSL_set_bio(ssl, bio, bio);
+    
+    SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
+
+    // Put our SSL into connect state
+    SSL_set_connect_state(ssl);
+
+    // Start the client handshake
+    ret = SSL_do_handshake(ssl);
+    if (ret<=0) {
+        jclass exception = (*env)->FindClass(env, "javax/net/ssl/SSLHandshakeException");
+        (*env)->ThrowNew(env, exception, ERR_reason_error_string(ERR_get_error()));
+    }
+
+    return (jlong)ssl;
+}
+
+JNIEXPORT void JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLSocketImpl_writeAppDataImpl
+  (JNIEnv *env, jclass clazz, jlong jssl, jbyteArray data, jint offset, jint len) {
+    SSL *ssl = (SSL*)jssl;
+    int ret;
+
+    jint byteSize = (*env)->GetArrayLength(env, data);
+    jbyte *buffer = (jbyte*) malloc(len * sizeof(jbyte*)); 
+    (*env)->GetByteArrayRegion(env, data, offset, len, buffer);
+
+    ret = SSL_write(ssl, (const void *)buffer, (int)len);
+    // Check len bytes were written to the socket and loop if not
+    if (ret == -1) {
+        // The socket has been closed
+        jclass exception = (*env)->FindClass(env, "java/net/SocketException");
+        (*env)->ThrowNew(env, exception, "Connection was reset");
+    }
+
+    free(buffer);
+}
+
+JNIEXPORT jbyte JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLSocketImpl_needAppDataImpl
+  (JNIEnv *env, jclass clazz, jlong jssl) {
+    SSL *ssl = (SSL*)jssl;
+    int ret;
+    jbyte data;
+
+    ret = SSL_read(ssl, (void *)&data, 1);
+    if (ret == -1) {
+        // The socket has been closed
+        jclass exception = (*env)->FindClass(env, "java/net/SocketException");
+        (*env)->ThrowNew(env, exception, "Connection was reset");
+        return -1;
+    }
+
+    return data;
+}

Propchange: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslSocket.c
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslSocket.h
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslSocket.h?rev=981527&view=auto
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslSocket.h
(added)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslSocket.h
Mon Aug  2 14:41:52 2010
@@ -0,0 +1,39 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+#include <jni.h>
+
+#ifndef _SERVERSOCKET_H
+#define _SERVERSOCKET_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+JNIEXPORT jlong JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLSocketImpl_sslAcceptImpl
+  (JNIEnv *, jclass, jlong, jobject);
+JNIEXPORT jlong JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLSocketImpl_sslConnectImpl
+  (JNIEnv *, jclass, jlong, jobject);
+JNIEXPORT void JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLSocketImpl_writeAppDataImpl
+  (JNIEnv *, jclass, jlong, jbyteArray, jint, jint);
+JNIEXPORT jbyte JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLSocketImpl_needAppDataImpl
+  (JNIEnv *, jclass, jlong);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

Propchange: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslSocket.h
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/windows/hyjsse.def
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/windows/hyjsse.def?rev=981527&view=auto
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/windows/hyjsse.def
(added)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/windows/hyjsse.def
Mon Aug  2 14:41:52 2010
@@ -0,0 +1,21 @@
+;  Licensed to the Apache Software Foundation (ASF) under one or more
+;  contributor license agreements.  See the NOTICE file distributed with
+;  this work for additional information regarding copyright ownership.
+;  The ASF licenses this file to You under the Apache License, Version 2.0
+;  (the "License"); you may not use this file except in compliance with
+;  the License.  You may obtain a copy of the License at
+;
+;     http://www.apache.org/licenses/LICENSE-2.0
+;
+;  Unless required by applicable law or agreed to in writing, software
+;  distributed under the License is distributed on an "AS IS" BASIS,
+;  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+;  See the License for the specific language governing permissions and
+;  limitations under the License.
+
+LIBRARY	HYJSSE
+
+SECTIONS
+	.data	READ WRITE
+	.text	EXECUTE READ
+

Added: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/windows/hyjsse.rc
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/windows/hyjsse.rc?rev=981527&view=auto
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/windows/hyjsse.rc
(added)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/windows/hyjsse.rc
Mon Aug  2 14:41:52 2010
@@ -0,0 +1,48 @@
+;
+; Licensed to the Apache Software Foundation (ASF) under one or more
+; contributor license agreements.  See the NOTICE file distributed with
+; this work for additional information regarding copyright ownership.
+; The ASF licenses this file to You under the Apache License, Version 2.0
+; (the "License"); you may not use this file except in compliance with
+; the License.  You may obtain a copy of the License at
+; 
+;     http://www.apache.org/licenses/LICENSE-2.0
+; 
+; Unless required by applicable law or agreed to in writing, software
+; distributed under the License is distributed on an "AS IS" BASIS,
+; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+; See the License for the specific language governing permissions and
+; limitations under the License.
+;
+
+#include <windows.h>
+#include <winver.h>
+
+VS_VERSION_INFO VERSIONINFO
+ FILEVERSION 0,1,0,0
+ PRODUCTVERSION 0,1,0,0
+ FILEFLAGSMASK 0x3fL
+ FILEFLAGS 0x0L
+ FILEOS VOS_NT_WINDOWS32
+ FILETYPE VFT_DLL
+ FILESUBTYPE 0x0L
+BEGIN
+	BLOCK "StringFileInfo"
+	BEGIN
+		BLOCK "040904b0"
+		BEGIN
+			VALUE "CompanyName", "The Apache Software Foundation.\0"
+			VALUE "FileDescription", "X-NET native code\0"
+			VALUE "FileVersion", "0.1\0"
+			VALUE "InternalName", "x-net\0"
+			VALUE "LegalCopyright", "(c) Copyright 1991, 2010 The Apache Software Foundation or its
licensors, as applicable.\0"
+			VALUE "OriginalFilename", "hyjsse.dll\0"
+			VALUE "ProductName", "Apache Harmony\0"
+			VALUE "ProductVersion", "0.1\0"
+		END
+	END
+	BLOCK "VarFileInfo"
+	BEGIN
+		VALUE "Translation", 0x0409, 1200
+	END
+END

Added: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/windows/makefile
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/windows/makefile?rev=981527&view=auto
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/windows/makefile
(added)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/windows/makefile
Mon Aug  2 14:41:52 2010
@@ -0,0 +1,42 @@
+#  Licensed to the Apache Software Foundation (ASF) under one or more
+#  contributor license agreements.  See the NOTICE file distributed with
+#  this work for additional information regarding copyright ownership.
+#  The ASF licenses this file to You under the Apache License, Version 2.0
+#  (the "License"); you may not use this file except in compliance with
+#  the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+#
+# Makefile for module 'jsse'
+#
+
+!include <$(HY_HDK)\build\make\defines.mak>
+
+OPENSSL_DIST = ..\..\openssl\# path to OpenSSL
+
+LIBBASE=hyjsse
+DLLNAME=$(DLLPATH)$(LIBBASE).dll
+LIBNAME=$(LIBPATH)$(LIBBASE).lib
+HYCFLAGS = $(HYCFLAGS) /I$(SHAREDSUB) /I$(OPENSSL_DIST)include
+HYLDFLAGS = $(HYLDFLAGS) -def:$(LIBBASE).def
+
+BUILDFILES = \
+  $(SHAREDSUB)jsse_copyright.obj $(SHAREDSUB)sslParameters.obj $(SHAREDSUB)sslSocket.obj
+
+VIRTFILES = hyjsse.res
+
+SYSLIBFILES = ws2_32.lib Iphlpapi.lib Mswsock.lib
+
+MDLLIBFILES = $(MDLLIBFILES) $(LIBPATH)vmi$(HY_LINKLIB_SUFFIX) \
+	$(OPENSSL_DIST)out32\libeay32.lib $(OPENSSL_DIST)out32\ssleay32.lib
+
+DLLBASE=0x13200000
+
+!include <$(HY_HDK)\build\make\rules.mak>

Propchange: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/windows/makefile
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message