Index: modules/security/src/main/java/windows/org/apache/harmony/security/provider/crypto/RandomBitsSupplier.java =================================================================== --- modules/security/src/main/java/windows/org/apache/harmony/security/provider/crypto/RandomBitsSupplier.java (revision 0) +++ modules/security/src/main/java/windows/org/apache/harmony/security/provider/crypto/RandomBitsSupplier.java (revision 0) @@ -0,0 +1,104 @@ +/* + * Copyright 2006 The Apache Software Software Foundation or its licensors, as applicable. + * + * Licensed 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.security.provider.crypto; + +import java.security.ProviderException; +import java.security.AccessController; + + +/** + * The static class providing access on Windows paltform + * to system means for generating true random bits.
+ * + * It uses a native method to get the random bits from CryptGenRandom. + * If the required library is not installed + * the provider shouldn't register the algorithm. + */ + +public class RandomBitsSupplier implements SHA1_Data { + + /** + * specification for native library + */ + private static native boolean getWindowsRandom(byte[] bytes, int numBytes); + + /** + * static field is "true" only if native library is linked + */ + private static boolean serviceAvailable; + + + static { + try { + AccessController.doPrivileged( + new java.security.PrivilegedAction() { + public Object run() throws UnsatisfiedLinkError { + System.loadLibrary(LIBRARY_NAME); + return null; + } + } + ); + } catch (UnsatisfiedLinkError e) { + serviceAvailable = false; + } + serviceAvailable = true; + } + + + /** + * The method is called by provider to determine if a device is available. + */ + static boolean isServiceAvailable() { + return serviceAvailable; + } + + + /** + * The method returns byte array containing random bits. + * + * @param + * numBytes - length of bytes requested + * @return + * byte array + * @throws + * InvalidArgumentException - if numBytes <= 0
+ * ProviderException - if some problem related to native library is discovered
+ */ + public static synchronized byte[] getRandomBits(int numBytes) { + + if ( numBytes <= 0 ) { + throw new IllegalArgumentException("numBytes <= 0 : " + numBytes); + } + + if ( !serviceAvailable ) { + throw new ProviderException( + "ATTENTION: service is not available : native library is not linked" ); + } + + byte[] myBytes = new byte[numBytes]; + + if ( !getWindowsRandom(myBytes, numBytes) ) { + + // it is unexpected result + throw new ProviderException( + "ATTENTION: getWindowsRandom(myBytes, numBytes) returned false" ); + } + + return myBytes; + } +} Index: modules/security/src/main/native/security/windows/hysecurity.def =================================================================== --- modules/security/src/main/native/security/windows/hysecurity.def (revision 0) +++ modules/security/src/main/native/security/windows/hysecurity.def (revision 0) @@ -0,0 +1,8 @@ +LIBRARY HYSECURITY + +SECTIONS + .data READ WRITE + .text EXECUTE READ + +EXPORTS + Java_org_apache_harmony_security_provider_crypto_RandomBitsSupplier_getWindowsRandom Index: modules/security/src/main/native/security/windows/hysecurity.rc =================================================================== --- modules/security/src/main/native/security/windows/hysecurity.rc (revision 0) +++ modules/security/src/main/native/security/windows/hysecurity.rc (revision 0) @@ -0,0 +1,47 @@ +; +; Copyright 2006 The Apache Software Foundation or its licensors, as applicable +; +; Licensed 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 +#include + +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", "Auth native code\0" + VALUE "FileVersion", "0.1\0" + VALUE "InternalName", "security\0" + VALUE "LegalCopyright", "(c) Copyright 2005, 2006 The Apache Software Foundation or its licensors, as applicable.\0" + VALUE "OriginalFilename", "hysecurity.dll\0" + VALUE "ProductName", "Apache Harmony\0" + VALUE "ProductVersion", "0.1\0" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x0409, 1200 + END +END Index: modules/security/src/main/native/security/windows/getWindowsRandom.c =================================================================== --- modules/security/src/main/native/security/windows/getWindowsRandom.c (revision 0) +++ modules/security/src/main/native/security/windows/getWindowsRandom.c (revision 0) @@ -0,0 +1,64 @@ +/* + * Copyright 2006 The Apache Software Foundation or its licensors, as applicable. + * + * Licensed 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. + */ + +#undef _WIN32_WINNT +#define _WIN32_WINNT 0x0500 + +#include + +#include +#include +#include + +#include + +#include "harmony.h" +#include "jni.h" + +JNIEXPORT jint JNICALL +Java_org_apache_harmony_security_provider_crypto_RandomBitsSupplier_getWindowsRandom(JNIEnv *env, jclass obj, jbyteArray bytes, jint numBytes) +{ + HCRYPTPROV hcrypt_provider; + + byte * random_bits; + int true = 1; + int false = 0; + + int b; + + b = CryptAcquireContext( &hcrypt_provider, NULL, NULL, PROV_DSS, CRYPT_VERIFYCONTEXT ); + + if ( !b ) { + return false; + } + + random_bits = malloc(numBytes); + + b = CryptGenRandom( hcrypt_provider, numBytes, random_bits ); + + if ( !b ){ + free(random_bits); + return false; + } + + b = CryptReleaseContext(hcrypt_provider, 0); + + (*env)->SetByteArrayRegion(env, bytes, 0, numBytes, (signed char*)random_bits); + free(random_bits); + + return true; +} + Index: modules/security/src/main/native/security/windows/makefile =================================================================== --- modules/security/src/main/native/security/windows/makefile (revision 0) +++ modules/security/src/main/native/security/windows/makefile (revision 0) @@ -0,0 +1,43 @@ +# Copyright 2006 The Apache Software Foundation or its licensors, as applicable +# +# Licensed 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.mak> + +LIBBASE=hysecurity + +DLLNAME=$(DLLPATH)$(LIBBASE).dll +LIBNAME=$(LIBPATH)$(LIBBASE).lib + +HYLDFLAGS = $(HYLDFLAGS) -def:$(LIBBASE).def +HYCFLAGS = $(HYCFLAGS) /I$(SHAREDSUB) /I$(SHARED)common /I..\zip \ + /I$(SHARED)fdlibm + +BUILDFILES = $(SHAREDSUB)security_copyright.obj getWindowsRandom.obj + +VIRTFILES = $(LIBBASE).res + +SYSLIBFILES = ws2_32.lib Iphlpapi.lib + +MDLLIBFILES = $(LIBPATH)hycommon.lib $(LIBPATH)hypool.lib + +DLLBASE=0x13900000 + +COMMENT=/comment:"Security component native code. (c) Copyright 1993, 2005 The Apache Software Foundation or its licensors, as applicable." + +!include <$(HY_HDK)\build\make\rules.mak> +