You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by od...@apache.org on 2010/08/18 17:50:00 UTC
svn commit: r986752 - in
/harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main:
java/org/apache/harmony/xnet/provider/jsse/ native/jsse/shared/
native/jsse/unix/
Author: odeakin
Date: Wed Aug 18 15:49:59 2010
New Revision: 986752
URL: http://svn.apache.org/viewvc?rev=986752&view=rev
Log:
Add implementation of RNG functions. To work around OpenSSL's global RNG callbacks use thread locals to store the SSLParameters from which we can check if a SecureRandom implementation has been set. If it has not, we fall back to the OpenSSL default functions.
Added:
harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/RNGHandler.java (with props)
Modified:
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/native/jsse/shared/jsse_rand.c
harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/jsse_rand.h
harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslParameters.c
harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslParameters.h
harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/unix/exports.txt
Added: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/RNGHandler.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/RNGHandler.java?rev=986752&view=auto
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/RNGHandler.java (added)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/RNGHandler.java Wed Aug 18 15:49:59 2010
@@ -0,0 +1,78 @@
+/*
+ * 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.
+ */
+
+package org.apache.harmony.xnet.provider.jsse;
+
+import java.security.SecureRandom;
+
+class RNGHandler {
+ private static SecureRandom getSecureRandom() {
+ SSLParameters sslParameters = SSLParameters.threadLocalParams.get();
+ if (sslParameters == null) {
+ return null;
+ }
+
+ SecureRandom secureRandom = sslParameters.getSecureRandom();
+ return secureRandom;
+ }
+
+ public static int randSeed(byte[] buf) {
+ SecureRandom secureRandom = getSecureRandom();
+ if (secureRandom == null) {
+ // Return -1 to tell the natives to use the OpenSSL default RNG
+ return -1;
+ }
+
+ secureRandom.setSeed(buf);
+ return 0;
+ }
+
+ public static byte[] randBytes(int num) {
+ SecureRandom secureRandom = getSecureRandom();
+ if (secureRandom == null) {
+ // Return null to tell the natives to use the OpenSSL default RNG
+ return null;
+ }
+
+ byte[] randomBytes = new byte[num];
+ secureRandom.nextBytes(randomBytes);
+ return randomBytes;
+ }
+
+ public static int randAdd(byte[] buf) {
+ SecureRandom secureRandom = getSecureRandom();
+ if (secureRandom == null) {
+ // Return -1 to tell the natives to use the OpenSSL default RNG
+ return -1;
+ }
+
+ secureRandom.setSeed(buf);
+ return 0;
+ }
+
+ public static byte[] randPseudoBytes(int num) {
+ SecureRandom secureRandom = getSecureRandom();
+ if (secureRandom == null) {
+ // Return null to tell the natives to use the OpenSSL default RNG
+ return null;
+ }
+
+ byte[] randomBytes = new byte[num];
+ secureRandom.nextBytes(randomBytes);
+ return randomBytes;
+ }
+}
Propchange: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/RNGHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
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=986752&r1=986751&r2=986752&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 Wed Aug 18 15:49:59 2010
@@ -49,8 +49,6 @@ public class SSLParameters {
private static X509KeyManager defaultKeyManager;
// default source of authentication trust decisions
private static X509TrustManager defaultTrustManager;
- // default source of random numbers
- private static SecureRandom defaultSecureRandom;
// default SSL parameters
private static SSLParameters defaultParameters;
@@ -93,16 +91,18 @@ public class SSLParameters {
// if the peer with this parameters allowed to cteate new SSL session
private boolean enable_session_creation = true;
+ static ThreadLocal<SSLParameters> threadLocalParams = new ThreadLocal<SSLParameters>();
// Native address of the OpenSSL SSL_CTX struct
private long SSL_CTX = 0;
- // Native method that gets the default list of cipher suites from OpenSSL
- private static native String[] getDefaultCipherSuites();
+ // Native method that returns the default list of cipher suites from OpenSSL
+ // and also sets the default RNG functions
+ private static native String[] initialiseDefaults();
static {
System.loadLibrary("hyjsse");
- supportedCipherSuites = getDefaultCipherSuites();
+ supportedCipherSuites = initialiseDefaults();
}
private static native long initialiseContext(byte[][] trustCerts, byte[] keyCert, byte[] privateKey);
@@ -198,14 +198,7 @@ public class SSLParameters {
throw new KeyManagementException(e);
}
// initialize secure random
- if (sr == null) {
- if (defaultSecureRandom == null) {
- defaultSecureRandom = new SecureRandom();
- }
- secureRandom = defaultSecureRandom;
- } else {
- secureRandom = sr;
- }
+ secureRandom = sr;
// Now setup our OpenSSL SSL_CTX with the various certificates
// First iterate through the trust certs storing their ASN1 form
@@ -242,7 +235,9 @@ public class SSLParameters {
}
}
+ threadLocalParams.set(this);
SSL_CTX = initialiseContext(tempCertsDER, keyCertDER, privateKeyDER);
+ threadLocalParams.remove();
}
protected static SSLParameters getDefault() throws KeyManagementException {
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=986752&r1=986751&r2=986752&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 Wed Aug 18 15:49:59 2010
@@ -435,6 +435,9 @@ public class SSLSocketImpl extends SSLSo
throw new IOException("Socket has already been closed.");
}
+ // Set the thread local SSLParameter so it can be used in the RNG callbacks
+ sslParameters.threadLocalParams.set(sslParameters);
+
if (!handshake_started) {
handshake_started = true;
if (sslParameters.getUseClientMode()) {
@@ -447,7 +450,7 @@ public class SSLSocketImpl extends SSLSo
if (logger != null) {
logger.println("SSLSocketImpl: SERVER accepting connection");
}
- sslAcceptImpl(SSL, impl.getFileDescriptor());
+ sslAcceptImpl(SSL, impl.getFileDescriptor());
}
}
@@ -470,6 +473,9 @@ public class SSLSocketImpl extends SSLSo
if (logger != null) {
logger.println("SSLSocketImpl.startHandshake: END");
}
+
+ // Remove our thread local SSLParameter now we are complete
+ sslParameters.threadLocalParams.remove();
}
Modified: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/jsse_rand.c
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/jsse_rand.c?rev=986752&r1=986751&r2=986752&view=diff
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/jsse_rand.c (original)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/jsse_rand.c Wed Aug 18 15:49:59 2010
@@ -28,60 +28,174 @@ void randAdd(const void *buf, int num, d
int randPseudoBytes(unsigned char *buf, int num);
int randStatus(void);
-JavaVM *javaVM;
+struct callbackFunctions_struct {
+ JavaVM *javaVM;
+ void (*origRandSeed)(const void *buf, int num);
+ int (*origRandBytes)(unsigned char *buf, int num);
+ void (*origRandAdd)(const void *buf, int num, double entropy);
+ int (*origRandPseudoBytes)(unsigned char *buf, int num);
+ jclass rngClass;
+ jmethodID randSeedMethod;
+ jmethodID randBytesMethod;
+ jmethodID randAddMethod;
+ jmethodID randPseudoMethod;
+};
+
+struct callbackFunctions_struct *callbacks;
+
+void initialiseRandMethod(JNIEnv *env) {
+ const RAND_METHOD *origRandMethod;
+ RAND_METHOD *newRandMethod;
+
+ origRandMethod = RAND_get_rand_method();
+ callbacks = malloc(sizeof(struct callbackFunctions_struct));
+ (*env)->GetJavaVM(env, &callbacks->javaVM);
+ callbacks->origRandSeed = origRandMethod->seed;
+ callbacks->origRandBytes = origRandMethod->bytes;
+ callbacks->origRandAdd = origRandMethod->add;
+ callbacks->origRandPseudoBytes = origRandMethod->pseudorand;
+
+ // In each of the following cases if the return value is NULL an exception should already have been
+ // thrown, so just return
+ callbacks->rngClass = (*env)->FindClass(env, "org/apache/harmony/xnet/provider/jsse/RNGHandler");
+ if (!callbacks->rngClass) return;
+
+ callbacks->randSeedMethod = (*env)->GetStaticMethodID(env, callbacks->rngClass, "randSeed", "([B)I");
+ if (!callbacks->randSeedMethod) return;
+ callbacks->randBytesMethod = (*env)->GetStaticMethodID(env, callbacks->rngClass, "randBytes", "(I)[B");
+ if (!callbacks->randBytesMethod) return;
+ callbacks->randAddMethod = (*env)->GetStaticMethodID(env, callbacks->rngClass, "randAdd", "([B)I");
+ if (!callbacks->randAddMethod) return;
+ callbacks->randPseudoMethod = (*env)->GetStaticMethodID(env, callbacks->rngClass, "randPseudoBytes", "(I)[B");
+ if (!callbacks->randPseudoMethod) return;
+
+ newRandMethod = malloc(sizeof(RAND_METHOD));
+ newRandMethod->seed = &randSeed;
+ newRandMethod->bytes = &randBytes;
+ newRandMethod->cleanup = &randCleanup;
+ newRandMethod->add = &randAdd;
+ newRandMethod->pseudorand = &randPseudoBytes;
+ newRandMethod->status = &randStatus;
-RAND_METHOD *getRandMethod(JavaVM *jvm) {
- RAND_METHOD *randMethod = malloc(sizeof(RAND_METHOD));
- randMethod->seed = &randSeed;
- randMethod->bytes = &randBytes;
- randMethod->cleanup = &randCleanup;
- randMethod->add = &randAdd;
- randMethod->pseudorand = &randPseudoBytes;
- randMethod->status = &randStatus;
-
- javaVM = jvm;
-
- return randMethod;
+ // TODO: Check for error returns here
+ RAND_set_rand_method(newRandMethod);
}
void randSeed(const void *buf, int num) {
- printf("randSeed with num=%d and javaVM=%p\n", num, javaVM);
+ JNIEnv *env;
+ int ret;
+ jbyteArray seedBuffer;
+
+ ret = (*callbacks->javaVM)->GetEnv(callbacks->javaVM, (void**)&env, JNI_VERSION_1_4);
+ if (ret) {
+ // TODO: throw appropriate exception
+ return;
+ }
- //(*javaVM)->GetEnv(javaVM, (void**)&env, JNI_VERSION_1_4);
+ seedBuffer = (*env)->NewByteArray(env, (jsize)num);
+ if ((*env)->ExceptionOccurred(env)) {
+ return;
+ }
- return;
+ (*env)->SetByteArrayRegion(env, seedBuffer, 0, (jsize)num, (jbyte*)buf);
+ if (!seedBuffer) {
+ return;
+ }
+
+ ret = (*env)->CallStaticIntMethod(env, callbacks->rngClass, callbacks->randSeedMethod, seedBuffer);
+ if (ret) {
+ // No SecureRandom implementation was set for this context - just call the OpenSSL default function
+ (*callbacks->origRandSeed)(buf, num);
+ }
}
int randBytes(unsigned char *buf, int num) {
- int i;
- printf("randBytes with num=%d and javaVM=%p\n", num, javaVM);
- for (i=0; i<num; i++) {
- buf[i] = 1;
+ JNIEnv *env;
+ int ret;
+ jbyteArray randBuffer;
+
+
+ ret = (*callbacks->javaVM)->GetEnv(callbacks->javaVM, (void**)&env, JNI_VERSION_1_4);
+ if (ret) {
+ // TODO: throw appropriate exception and return
+ return 0;
+ }
+
+ randBuffer = (jbyteArray)(*env)->CallStaticObjectMethod(env, callbacks->rngClass, callbacks->randBytesMethod, (jint)num);
+ if (!randBuffer) {
+ // No SecureRandom implementation was set for this context - just call the OpenSSL default function
+ ret = (*callbacks->origRandBytes)(buf, num);
+ if (!ret) {
+ // TODO: throw appropriate exception here
+ return 0;
+ }
+ } else {
+ (*env)->GetByteArrayRegion(env, randBuffer, 0, (jint)num, (jbyte*)buf);
}
+
return 1;
}
void randCleanup() {
- printf("randCleanup and javaVM=%p\n", javaVM);
+ // Do nothing and return
return;
}
void randAdd(const void *buf, int num, double entropy) {
- printf("randAdd with num=%d and entropy=%f and javaVM=%p\n", num, entropy, javaVM);
+ JNIEnv *env;
+ int ret;
+ jbyteArray seedBuffer;
+
+ ret = (*callbacks->javaVM)->GetEnv(callbacks->javaVM, (void**)&env, JNI_VERSION_1_4);
+ if (ret) {
+ // TODO: throw appropriate exception
+ return;
+ }
- return;
+ seedBuffer = (*env)->NewByteArray(env, (jsize)num);
+ if ((*env)->ExceptionOccurred(env)) {
+ return;
+ }
+
+ (*env)->SetByteArrayRegion(env, seedBuffer, 0, (jsize)num, (jbyte*)buf);
+ if (!seedBuffer) {
+ return;
+ }
+
+ ret = (*env)->CallStaticIntMethod(env, callbacks->rngClass, callbacks->randAddMethod, seedBuffer);
+ if (ret) {
+ // No SecureRandom implementation was set for this context - just call the OpenSSL default function
+ (*callbacks->origRandAdd)(buf, num, entropy);
+ }
}
-int randPseudoBytes(unsigned char *buf, int num) {
- int i;
- printf("randPseudoBytes with num=%d and javaVM=%p\n", num, javaVM);
- for (i=0; i<num; i++) {
- buf[i] = 1;
+int randPseudoBytes(unsigned char *buf, int num) {
+ JNIEnv *env;
+ int ret;
+ jbyteArray randBuffer;
+
+ ret = (*callbacks->javaVM)->GetEnv(callbacks->javaVM, (void**)&env, JNI_VERSION_1_4);
+ if (ret) {
+ // TODO: throw appropriate exception and return
+ return 0;
+ }
+
+ randBuffer = (jbyteArray)(*env)->CallStaticObjectMethod(env, callbacks->rngClass, callbacks->randPseudoMethod, (jint)num);
+ if (!randBuffer) {
+ // No SecureRandom implementation was set for this context - just call the OpenSSL default function
+ ret = (*callbacks->origRandPseudoBytes)(buf, num);
+ if (!ret) {
+ // TODO: throw appropriate exception here
+ return 0;
+ }
+ } else {
+ (*env)->GetByteArrayRegion(env, randBuffer, 0, (jint)num, (jbyte*)buf);
}
+
return 1;
}
int randStatus() {
- printf("randStatus and javaVM=%p\n", javaVM);
- return 0;
+ // Do nothing and return success
+ return 1;
}
Modified: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/jsse_rand.h
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/jsse_rand.h?rev=986752&r1=986751&r2=986752&view=diff
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/jsse_rand.h (original)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/jsse_rand.h Wed Aug 18 15:49:59 2010
@@ -15,11 +15,11 @@
* limitations under the License.
*/
-#include "openssl/rand.h"
+#include "jni.h"
-#ifndef _RAND_H
-#define _RAND_H
+#ifndef _JSSE_RAND_H
+#define _JSSE_RAND_H
-RAND_METHOD *getRandMethod();
+void initialiseRandMethod(JNIEnv*);
#endif
Modified: 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=986752&r1=986751&r2=986752&view=diff
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslParameters.c (original)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslParameters.c Wed Aug 18 15:49:59 2010
@@ -24,7 +24,7 @@
#include "openssl/err.h"
#include "jsse_rand.h"
-JNIEXPORT jobjectArray JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLParameters_getDefaultCipherSuites
+JNIEXPORT jobjectArray JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLParameters_initialiseDefaults
(JNIEnv *env, jclass clazz)
{
SSL_CTX *context;
@@ -64,6 +64,10 @@ JNIEXPORT jobjectArray JNICALL Java_org_
SSL_free(ssl);
SSL_CTX_free(context);
+ // Initialise our global RNG functions to call into RNGHandler
+ initialiseRandMethod(env);
+
+ // Return the array of default cipher suites
return stringArray;
}
@@ -77,8 +81,6 @@ JNIEXPORT jlong JNICALL Java_org_apache_
X509 *x509cert;
const unsigned char *temp;
int ret;
- RAND_METHOD *randMethod;
- JavaVM *jvm;
context = SSL_CTX_new(SSLv23_method());
@@ -152,11 +154,6 @@ JNIEXPORT jlong JNICALL Java_org_apache_
}
free(certBuffer);
}
-
- // TODO: Check for error return here
- (*env)->GetJavaVM(env, &jvm);
- randMethod = getRandMethod(jvm);
- RAND_set_rand_method(randMethod);
return addr2jlong(context);
}
Modified: 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=986752&r1=986751&r2=986752&view=diff
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslParameters.h (original)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/shared/sslParameters.h Wed Aug 18 15:49:59 2010
@@ -34,7 +34,7 @@ extern "C" {
#define REQUEST_CLIENT_AUTH 2
#define REQUIRE_CLIENT_AUTH 4
-JNIEXPORT jobjectArray JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLParameters_getDefaultCipherSuites
+JNIEXPORT jobjectArray JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLParameters_initialiseDefaults
(JNIEnv *, jclass);
JNIEXPORT jlong JNICALL Java_org_apache_harmony_xnet_provider_jsse_SSLParameters_initialiseContext
(JNIEnv *, jclass, jobjectArray, jbyteArray, jbyteArray);
Modified: harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/unix/exports.txt
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/unix/exports.txt?rev=986752&r1=986751&r2=986752&view=diff
==============================================================================
--- harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/unix/exports.txt (original)
+++ harmony/enhanced/java/branches/omd/classlib/modules/x-net/src/main/native/jsse/unix/exports.txt Wed Aug 18 15:49:59 2010
@@ -1,7 +1,7 @@
+Java_org_apache_harmony_xnet_provider_jsse_SSLParameters_initialiseDefaults
Java_org_apache_harmony_xnet_provider_jsse_SSLParameters_initialiseContext
Java_org_apache_harmony_xnet_provider_jsse_SSLParameters_setEnabledProtocolsImpl
Java_org_apache_harmony_xnet_provider_jsse_SSLParameters_setClientAuthImpl
-Java_org_apache_harmony_xnet_provider_jsse_SSLParameters_getSupportedCipherSuitesImpl
Java_org_apache_harmony_xnet_provider_jsse_SSLParameters_setEnabledCipherSuitesImpl
Java_org_apache_harmony_xnet_provider_jsse_SSLSessionImpl_initialiseSession
Java_org_apache_harmony_xnet_provider_jsse_SSLSocketImpl_initImpl