|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Red Hat, Inc. |
| 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 <dlfcn.h> |
| 27 | +#include <jni.h> |
| 28 | +#include <jni_util.h> |
| 29 | +#include <stdio.h> |
| 30 | + |
| 31 | +#ifdef SYSCONF_NSS |
| 32 | +#include <nss3/pk11pub.h> |
| 33 | +#endif //SYSCONF_NSS |
| 34 | + |
| 35 | +#include "java_security_SystemConfigurator.h" |
| 36 | + |
| 37 | +#define FIPS_ENABLED_PATH "/proc/sys/crypto/fips_enabled" |
| 38 | +#define MSG_MAX_SIZE 96 |
| 39 | + |
| 40 | +static jmethodID debugPrintlnMethodID = NULL; |
| 41 | +static jobject debugObj = NULL; |
| 42 | + |
| 43 | +static void throwIOException(JNIEnv *env, const char *msg); |
| 44 | +static void dbgPrint(JNIEnv *env, const char* msg); |
| 45 | + |
| 46 | +/* |
| 47 | + * Class: java_security_SystemConfigurator |
| 48 | + * Method: JNI_OnLoad |
| 49 | + */ |
| 50 | +JNIEXPORT jint JNICALL DEF_JNI_OnLoad(JavaVM *vm, void *reserved) |
| 51 | +{ |
| 52 | + JNIEnv *env; |
| 53 | + jclass sysConfCls, debugCls; |
| 54 | + jfieldID sdebugFld; |
| 55 | + |
| 56 | + if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_2) != JNI_OK) { |
| 57 | + return JNI_EVERSION; /* JNI version not supported */ |
| 58 | + } |
| 59 | + |
| 60 | + sysConfCls = (*env)->FindClass(env,"java/security/SystemConfigurator"); |
| 61 | + if (sysConfCls == NULL) { |
| 62 | + printf("libsystemconf: SystemConfigurator class not found\n"); |
| 63 | + return JNI_ERR; |
| 64 | + } |
| 65 | + sdebugFld = (*env)->GetStaticFieldID(env, sysConfCls, |
| 66 | + "sdebug", "Lsun/security/util/Debug;"); |
| 67 | + if (sdebugFld == NULL) { |
| 68 | + printf("libsystemconf: SystemConfigurator::sdebug field not found\n"); |
| 69 | + return JNI_ERR; |
| 70 | + } |
| 71 | + debugObj = (*env)->GetStaticObjectField(env, sysConfCls, sdebugFld); |
| 72 | + if (debugObj != NULL) { |
| 73 | + debugCls = (*env)->FindClass(env,"sun/security/util/Debug"); |
| 74 | + if (debugCls == NULL) { |
| 75 | + printf("libsystemconf: Debug class not found\n"); |
| 76 | + return JNI_ERR; |
| 77 | + } |
| 78 | + debugPrintlnMethodID = (*env)->GetMethodID(env, debugCls, |
| 79 | + "println", "(Ljava/lang/String;)V"); |
| 80 | + if (debugPrintlnMethodID == NULL) { |
| 81 | + printf("libsystemconf: Debug::println(String) method not found\n"); |
| 82 | + return JNI_ERR; |
| 83 | + } |
| 84 | + debugObj = (*env)->NewGlobalRef(env, debugObj); |
| 85 | + } |
| 86 | + |
| 87 | + return (*env)->GetVersion(env); |
| 88 | +} |
| 89 | + |
| 90 | +/* |
| 91 | + * Class: java_security_SystemConfigurator |
| 92 | + * Method: JNI_OnUnload |
| 93 | + */ |
| 94 | +JNIEXPORT void JNICALL DEF_JNI_OnUnload(JavaVM *vm, void *reserved) |
| 95 | +{ |
| 96 | + JNIEnv *env; |
| 97 | + |
| 98 | + if (debugObj != NULL) { |
| 99 | + if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_2) != JNI_OK) { |
| 100 | + return; /* Should not happen */ |
| 101 | + } |
| 102 | + (*env)->DeleteGlobalRef(env, debugObj); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +JNIEXPORT jboolean JNICALL Java_java_security_SystemConfigurator_getSystemFIPSEnabled |
| 107 | + (JNIEnv *env, jclass cls) |
| 108 | +{ |
| 109 | + int fips_enabled; |
| 110 | + char msg[MSG_MAX_SIZE]; |
| 111 | + int msg_bytes; |
| 112 | + |
| 113 | +#ifdef SYSCONF_NSS |
| 114 | + |
| 115 | + dbgPrint(env, "getSystemFIPSEnabled: calling SECMOD_GetSystemFIPSEnabled"); |
| 116 | + fips_enabled = SECMOD_GetSystemFIPSEnabled(); |
| 117 | + msg_bytes = snprintf(msg, MSG_MAX_SIZE, "getSystemFIPSEnabled:" \ |
| 118 | + " SECMOD_GetSystemFIPSEnabled returned 0x%x", fips_enabled); |
| 119 | + if (msg_bytes > 0 && msg_bytes < MSG_MAX_SIZE) { |
| 120 | + dbgPrint(env, msg); |
| 121 | + } else { |
| 122 | + dbgPrint(env, "getSystemFIPSEnabled: cannot render" \ |
| 123 | + " SECMOD_GetSystemFIPSEnabled return value"); |
| 124 | + } |
| 125 | + return (fips_enabled == 1 ? JNI_TRUE : JNI_FALSE); |
| 126 | + |
| 127 | +#else // SYSCONF_NSS |
| 128 | + |
| 129 | + FILE *fe; |
| 130 | + |
| 131 | + dbgPrint(env, "getSystemFIPSEnabled: reading " FIPS_ENABLED_PATH); |
| 132 | + if ((fe = fopen(FIPS_ENABLED_PATH, "r")) == NULL) { |
| 133 | + throwIOException(env, "Cannot open " FIPS_ENABLED_PATH); |
| 134 | + } |
| 135 | + fips_enabled = fgetc(fe); |
| 136 | + fclose(fe); |
| 137 | + if (fips_enabled == EOF) { |
| 138 | + throwIOException(env, "Cannot read " FIPS_ENABLED_PATH); |
| 139 | + } |
| 140 | + msg_bytes = snprintf(msg, MSG_MAX_SIZE, "getSystemFIPSEnabled:" \ |
| 141 | + " read character is '%c'", fips_enabled); |
| 142 | + if (msg_bytes > 0 && msg_bytes < MSG_MAX_SIZE) { |
| 143 | + dbgPrint(env, msg); |
| 144 | + } else { |
| 145 | + dbgPrint(env, "getSystemFIPSEnabled: cannot render" \ |
| 146 | + " read character"); |
| 147 | + } |
| 148 | + return (fips_enabled == '1' ? JNI_TRUE : JNI_FALSE); |
| 149 | + |
| 150 | +#endif // SYSCONF_NSS |
| 151 | +} |
| 152 | + |
| 153 | +static void throwIOException(JNIEnv *env, const char *msg) |
| 154 | +{ |
| 155 | + jclass cls = (*env)->FindClass(env, "java/io/IOException"); |
| 156 | + if (cls != 0) |
| 157 | + (*env)->ThrowNew(env, cls, msg); |
| 158 | +} |
| 159 | + |
| 160 | +static void dbgPrint(JNIEnv *env, const char* msg) |
| 161 | +{ |
| 162 | + jstring jMsg; |
| 163 | + if (debugObj != NULL) { |
| 164 | + jMsg = (*env)->NewStringUTF(env, msg); |
| 165 | + CHECK_NULL(jMsg); |
| 166 | + (*env)->CallVoidMethod(env, debugObj, debugPrintlnMethodID, jMsg); |
| 167 | + } |
| 168 | +} |
0 commit comments