1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <windows.h>
  27 
  28 #include <jni.h>
  29 #include "jni_util.h"
  30 #include "sun_misc_SystemRandomImpl.h"
  31 
  32 static BOOLEAN (APIENTRY *rtlGenRandomFn)(void*, ULONG) = 0;
  33 
  34 /*
  35  * Load library (increment reference count), obtain the rtlGenRandomFn
  36  * function pointer and return a library handle
  37  */
  38 JNIEXPORT jlong JNICALL Java_sun_misc_SystemRandomImpl_init0
  39   (JNIEnv *env, jclass clazz)
  40 {
  41     HMODULE hLib = LoadLibraryA("ADVAPI32.DLL");
  42     if (!hLib) {
  43         JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", "Can't load ADVAPI32.DLL");
  44         return 0;
  45     }
  46 
  47     if (!rtlGenRandomFn) {
  48         rtlGenRandomFn = (BOOLEAN (APIENTRY *)(void*,ULONG))GetProcAddress(hLib, "SystemFunction036");
  49     }
  50 
  51     if (!rtlGenRandomFn) {
  52         FreeLibrary(hLib);
  53         JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", "Can't obtain ADVAPI32!RtlGenRandom function.");
  54         return 0;
  55     }
  56 
  57     return (jlong)hLib;
  58 }
  59 
  60 /*
  61  * Obtain random bytes
  62  */
  63 JNIEXPORT void JNICALL Java_sun_misc_SystemRandomImpl_getBytes0
  64   (JNIEnv *env, jclass clazz, jbyteArray randArray)
  65 {
  66     jsize numBytes = (*env)->GetArrayLength(env, randArray);
  67     jbyte* randBytes = (*env)->GetByteArrayElements(env, randArray, NULL);
  68     if (randBytes == NULL) {
  69         JNU_ThrowByName(env, "java/lang/RuntimeException", "Can't obtain byte[] elements.");
  70         return;
  71     }
  72 
  73     if (!rtlGenRandomFn(randBytes, numBytes)) {
  74         (*env)->ReleaseByteArrayElements(env, randArray, randBytes, 0);
  75         JNU_ThrowByName(env, "java/lang/RuntimeException", "RtlGenRandom function failed.");
  76         return;
  77     }
  78 
  79     (*env)->ReleaseByteArrayElements(env, randArray, randBytes, 0);
  80 }
  81 
  82 /*
  83  * Unload the library (decrement reference count)
  84  */
  85 JNIEXPORT void JNICALL Java_sun_misc_SystemRandomImpl_deinit0
  86   (JNIEnv *env, jclass clazz, jlong libraryHandle)
  87 {
  88     FreeLibrary((HMODULE) libraryHandle);
  89 }