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 2008/01/07 15:29:08 UTC
svn commit: r609614 - in
/harmony/enhanced/classlib/trunk/modules/security/src/main:
java/unix/org/apache/harmony/security/provider/crypto/
native/security/shared/ native/security/unix/ native/security/windows/
Author: odeakin
Date: Mon Jan 7 06:29:06 2008
New Revision: 609614
URL: http://svn.apache.org/viewvc?rev=609614&view=rev
Log:
Fall back to Unix system random() API calls if there are no /dev/*random devices present.
Added:
harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/shared/
harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/shared/security_copyright.c
- copied, changed from r609598, harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/windows/security_copyright.c
harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/unix/
harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/unix/exports.txt (with props)
harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/unix/getUnixSystemRandom.c (with props)
harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/unix/makefile (with props)
Removed:
harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/windows/security_copyright.c
Modified:
harmony/enhanced/classlib/trunk/modules/security/src/main/java/unix/org/apache/harmony/security/provider/crypto/RandomBitsSupplier.java
harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/windows/makefile
Modified: harmony/enhanced/classlib/trunk/modules/security/src/main/java/unix/org/apache/harmony/security/provider/crypto/RandomBitsSupplier.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/security/src/main/java/unix/org/apache/harmony/security/provider/crypto/RandomBitsSupplier.java?rev=609614&r1=609613&r2=609614&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/security/src/main/java/unix/org/apache/harmony/security/provider/crypto/RandomBitsSupplier.java (original)
+++ harmony/enhanced/classlib/trunk/modules/security/src/main/java/unix/org/apache/harmony/security/provider/crypto/RandomBitsSupplier.java Mon Jan 7 06:29:06 2008
@@ -60,7 +60,7 @@
/**
* value of field is "true" only if a device is available
*/
- private static boolean serviceAvailable;
+ private static boolean serviceAvailable = false;
static {
@@ -76,16 +76,25 @@
bis = new BufferedInputStream(
new FileInputStream(file));
randomFile = file;
+ serviceAvailable = true;
return null;
}
} catch (FileNotFoundException e) {
}
}
+
+ // If we have come out of the above loop, then we have been unable to
+ // access /dev/*random, so try to fall back to using the system random() API
+ try {
+ System.loadLibrary(LIBRARY_NAME);
+ serviceAvailable = true;
+ } catch (UnsatisfiedLinkError e) {
+ serviceAvailable = false;
+ }
return null;
}
}
);
- serviceAvailable = (bis != null);
}
@@ -98,12 +107,12 @@
/**
- * On the Linux platform with "random" devices available,
+ * On platforms with "random" devices available,
* the method reads random bytes from the device. <BR>
*
* In case of any runtime failure ProviderException gets thrown.
*/
- private static synchronized byte[] getLinuxRandomBits(int numBytes) {
+ private static synchronized byte[] getUnixDeviceRandom(int numBytes) {
byte[] bytes = new byte[numBytes];
@@ -118,7 +127,6 @@
// the below case should not occur because /dev/random or /dev/urandom is a special file
// hence, if it is happened there is some internal problem
- //
if ( bytesRead == -1 ) {
throw new ProviderException(
Messages.getString("security.193") ); //$NON-NLS-1$
@@ -136,7 +144,6 @@
// actually there should be no IOException because device is a special file;
// hence, there is either some internal problem or, for instance,
// device was removed in runtime, or something else
- //
throw new ProviderException(
Messages.getString("security.194"), e ); //$NON-NLS-1$
}
@@ -145,6 +152,15 @@
/**
+ * On platforms with no "random" devices available, this native
+ * method uses system API calls to generate random numbers<BR>
+ *
+ * In case of any runtime failure ProviderException gets thrown.
+ */
+ private static native synchronized boolean getUnixSystemRandom(byte[] randomBits, int numBytes);
+
+
+ /**
* The method returns byte array of requested length provided service is available.
* ProviderException gets thrown otherwise.
*
@@ -161,12 +177,27 @@
throw new IllegalArgumentException(Messages.getString("security.195", numBytes)); //$NON-NLS-1$
}
+ // We have been unable to get a random device or fall back to the
+ // native security module code - throw an exception.
if ( !serviceAvailable ) {
throw new ProviderException(
Messages.getString("security.196")); //$NON-NLS-1$
}
- return getLinuxRandomBits(numBytes);
- }
+ byte[] randomBits;
+ if (bis != null) {
+ // Random devices exist
+ randomBits = getUnixDeviceRandom(numBytes);
+ } else {
+ // No random devices exist, use the system random() call
+ randomBits = new byte[numBytes];
+ if (!getUnixSystemRandom(randomBits, numBytes)) {
+ // Even the system call has failed, throw an exception
+ throw new ProviderException(
+ Messages.getString("security.196") ); //$NON-NLS-1$
+ }
+ }
+ return randomBits;
+ }
}
Copied: harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/shared/security_copyright.c (from r609598, harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/windows/security_copyright.c)
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/shared/security_copyright.c?p2=harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/shared/security_copyright.c&p1=harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/windows/security_copyright.c&r1=609598&r2=609614&rev=609614&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/windows/security_copyright.c (original)
+++ harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/shared/security_copyright.c Mon Jan 7 06:29:06 2008
@@ -18,4 +18,4 @@
/* A copyright string included in each DLL and executable */
const char hyCopyright[] =
- "(c) Copyright 2006 The Apache Software Foundation or its licensors, as applicable.";
+ "(c) Copyright 2006, 2007 The Apache Software Foundation or its licensors, as applicable.";
Added: harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/unix/exports.txt
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/unix/exports.txt?rev=609614&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/unix/exports.txt (added)
+++ harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/unix/exports.txt Mon Jan 7 06:29:06 2008
@@ -0,0 +1 @@
+Java_org_apache_harmony_security_provider_crypto_RandomBitsSupplier_getUnixSystemRandom
Propchange: harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/unix/exports.txt
------------------------------------------------------------------------------
svn:eol-style = native
Added: harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/unix/getUnixSystemRandom.c
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/unix/getUnixSystemRandom.c?rev=609614&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/unix/getUnixSystemRandom.c (added)
+++ harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/unix/getUnixSystemRandom.c Mon Jan 7 06:29:06 2008
@@ -0,0 +1,58 @@
+/*
+ * 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 <stdlib.h>
+#include <time.h>
+#include <limits.h>
+
+#include "vmi.h"
+#include "jni.h"
+#include "hyport.h"
+#include "hycomp.h"
+
+JNIEXPORT jint JNICALL
+Java_org_apache_harmony_security_provider_crypto_RandomBitsSupplier_getUnixSystemRandom(JNIEnv *env, jclass obj, jbyteArray bytes, jint numBytes)
+{
+ PORT_ACCESS_FROM_ENV(env);
+ jbyte *randomBits = hymem_allocate_memory(numBytes * sizeof(jbyte));
+
+ clock_t processTime = clock();
+ time_t currentTime = time(NULL);
+
+ int i;
+
+ // Check for error return values
+ if ((!randomBits) || (-1 == processTime) || (-1 == currentTime)) {
+ return 0;
+ }
+
+ // Seed the random number generator
+ srandom(abs((currentTime * processTime * (long)randomBits) % INT_MAX));
+
+ // Generate numBytes of random numbers
+ for (i=0; i<numBytes; i++) {
+ randomBits[i] = (jbyte) (random() % 128);
+ }
+
+ // Copy the randomly generated bytes into the Java byte array
+ (*env)->SetByteArrayRegion(env, bytes, 0, numBytes, randomBits);
+
+ hymem_free_memory(randomBits);
+
+ return 1;
+}
+
Propchange: harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/unix/getUnixSystemRandom.c
------------------------------------------------------------------------------
svn:eol-style = native
Added: harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/unix/makefile
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/unix/makefile?rev=609614&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/unix/makefile (added)
+++ harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/unix/makefile Mon Jan 7 06:29:06 2008
@@ -0,0 +1,30 @@
+# 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 'security'
+#
+
+include $(HY_HDK)/build/make/defines.mk
+
+BUILDFILES = \
+ $(SHAREDSUB)security_copyright.o getUnixSystemRandom.o
+
+DLLNAME = ../libhysecurity$(HY_SHLIB_SUFFIX)
+EXPNAME = HYSECURITY_0.1
+
+MDLLIBFILES += $(LIBPATH)libvmi$(HY_LINKLIB_SUFFIX)
+
+include $(HY_HDK)/build/make/rules.mk
Propchange: harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/unix/makefile
------------------------------------------------------------------------------
svn:eol-style = native
Modified: harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/windows/makefile
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/windows/makefile?rev=609614&r1=609613&r2=609614&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/windows/makefile (original)
+++ harmony/enhanced/classlib/trunk/modules/security/src/main/native/security/windows/makefile Mon Jan 7 06:29:06 2008
@@ -22,7 +22,7 @@
LIBBASE=hysecurity
DLLNAME=..\$(LIBBASE).dll
LIBNAME=$(LIBPATH)$(LIBBASE).lib
-BUILDFILES = security_copyright.obj getWindowsRandom.obj
+BUILDFILES = $(SHAREDSUB)security_copyright.obj getWindowsRandom.obj
VIRTFILES = $(LIBBASE).res
HYLDFLAGS = $(HYLDFLAGS) -def:$(LIBBASE).def
@@ -30,6 +30,6 @@
MDLLIBFILES = $(LIBPATH)hycommon$(HY_LINKLIB_SUFFIX) $(LIBPATH)hypool$(HY_LINKLIB_SUFFIX)
DLLBASE=0x1300000
-COMMENT=/comment:"Security component native code. (c) Copyright 2006 The Apache Software Foundation or its licensors, as applicable."
+COMMENT=/comment:"Security component native code. (c) Copyright 2006,2007 The Apache Software Foundation or its licensors, as applicable."
!include <$(HY_HDK)\build\make\rules.mak>