|
|
988ad7 |
commit e2be09f982af1cc05f5e6556d51900bca4757416
|
|
|
988ad7 |
Author: Andrew Hughes <gnu.andrew@redhat.com>
|
|
|
988ad7 |
Date: Mon Feb 28 05:30:32 2022 +0000
|
|
|
988ad7 |
|
|
|
988ad7 |
RH2051605: Detect NSS at Runtime for FIPS detection
|
|
|
988ad7 |
|
|
|
988ad7 |
diff --git openjdk.orig/src/java.base/linux/native/libsystemconf/systemconf.c openjdk/src/java.base/linux/native/libsystemconf/systemconf.c
|
|
|
988ad7 |
index 34d0ff0ce91..8dcb7d9073f 100644
|
|
|
988ad7 |
--- openjdk.orig/src/java.base/linux/native/libsystemconf/systemconf.c
|
|
|
988ad7 |
+++ openjdk/src/java.base/linux/native/libsystemconf/systemconf.c
|
|
|
988ad7 |
@@ -23,25 +23,99 @@
|
|
|
988ad7 |
* questions.
|
|
|
988ad7 |
*/
|
|
|
988ad7 |
|
|
|
988ad7 |
-#include <dlfcn.h>
|
|
|
988ad7 |
#include <jni.h>
|
|
|
988ad7 |
#include <jni_util.h>
|
|
|
988ad7 |
+#include "jvm_md.h"
|
|
|
988ad7 |
#include <stdio.h>
|
|
|
988ad7 |
|
|
|
988ad7 |
#ifdef SYSCONF_NSS
|
|
|
988ad7 |
#include <nss3/pk11pub.h>
|
|
|
988ad7 |
+#else
|
|
|
988ad7 |
+#include <dlfcn.h>
|
|
|
988ad7 |
#endif //SYSCONF_NSS
|
|
|
988ad7 |
|
|
|
988ad7 |
#include "java_security_SystemConfigurator.h"
|
|
|
988ad7 |
|
|
|
988ad7 |
+#define MSG_MAX_SIZE 256
|
|
|
988ad7 |
#define FIPS_ENABLED_PATH "/proc/sys/crypto/fips_enabled"
|
|
|
988ad7 |
-#define MSG_MAX_SIZE 96
|
|
|
988ad7 |
|
|
|
988ad7 |
+typedef int (SECMOD_GET_SYSTEM_FIPS_ENABLED_TYPE)(void);
|
|
|
988ad7 |
+
|
|
|
988ad7 |
+static SECMOD_GET_SYSTEM_FIPS_ENABLED_TYPE *getSystemFIPSEnabled;
|
|
|
988ad7 |
static jmethodID debugPrintlnMethodID = NULL;
|
|
|
988ad7 |
static jobject debugObj = NULL;
|
|
|
988ad7 |
|
|
|
988ad7 |
-static void throwIOException(JNIEnv *env, const char *msg);
|
|
|
988ad7 |
-static void dbgPrint(JNIEnv *env, const char* msg);
|
|
|
988ad7 |
+static void dbgPrint(JNIEnv *env, const char* msg)
|
|
|
988ad7 |
+{
|
|
|
988ad7 |
+ jstring jMsg;
|
|
|
988ad7 |
+ if (debugObj != NULL) {
|
|
|
988ad7 |
+ jMsg = (*env)->NewStringUTF(env, msg);
|
|
|
988ad7 |
+ CHECK_NULL(jMsg);
|
|
|
988ad7 |
+ (*env)->CallVoidMethod(env, debugObj, debugPrintlnMethodID, jMsg);
|
|
|
988ad7 |
+ }
|
|
|
988ad7 |
+}
|
|
|
988ad7 |
+
|
|
|
988ad7 |
+static void throwIOException(JNIEnv *env, const char *msg)
|
|
|
988ad7 |
+{
|
|
|
988ad7 |
+ jclass cls = (*env)->FindClass(env, "java/io/IOException");
|
|
|
988ad7 |
+ if (cls != 0)
|
|
|
988ad7 |
+ (*env)->ThrowNew(env, cls, msg);
|
|
|
988ad7 |
+}
|
|
|
988ad7 |
+
|
|
|
988ad7 |
+static void handle_msg(JNIEnv *env, const char* msg, int msg_bytes)
|
|
|
988ad7 |
+{
|
|
|
988ad7 |
+ if (msg_bytes > 0 && msg_bytes < MSG_MAX_SIZE) {
|
|
|
988ad7 |
+ dbgPrint(env, msg);
|
|
|
988ad7 |
+ } else {
|
|
|
988ad7 |
+ dbgPrint(env, "systemconf: cannot render message");
|
|
|
988ad7 |
+ }
|
|
|
988ad7 |
+}
|
|
|
988ad7 |
+
|
|
|
988ad7 |
+// Only used when NSS is not linked at build time
|
|
|
988ad7 |
+#ifndef SYSCONF_NSS
|
|
|
988ad7 |
+
|
|
|
988ad7 |
+static void *nss_handle;
|
|
|
988ad7 |
+
|
|
|
988ad7 |
+static jboolean loadNSS(JNIEnv *env)
|
|
|
988ad7 |
+{
|
|
|
988ad7 |
+ char msg[MSG_MAX_SIZE];
|
|
|
988ad7 |
+ int msg_bytes;
|
|
|
988ad7 |
+ const char* errmsg;
|
|
|
988ad7 |
+
|
|
|
988ad7 |
+ nss_handle = dlopen(JNI_LIB_NAME("nss3"), RTLD_LAZY);
|
|
|
988ad7 |
+ if (nss_handle == NULL) {
|
|
|
988ad7 |
+ errmsg = dlerror();
|
|
|
988ad7 |
+ msg_bytes = snprintf(msg, MSG_MAX_SIZE, "loadNSS: dlopen: %s\n",
|
|
|
988ad7 |
+ errmsg);
|
|
|
988ad7 |
+ handle_msg(env, msg, msg_bytes);
|
|
|
988ad7 |
+ return JNI_FALSE;
|
|
|
988ad7 |
+ }
|
|
|
988ad7 |
+ dlerror(); /* Clear errors */
|
|
|
988ad7 |
+ getSystemFIPSEnabled = (SECMOD_GET_SYSTEM_FIPS_ENABLED_TYPE*)dlsym(nss_handle, "SECMOD_GetSystemFIPSEnabled");
|
|
|
988ad7 |
+ if ((errmsg = dlerror()) != NULL) {
|
|
|
988ad7 |
+ msg_bytes = snprintf(msg, MSG_MAX_SIZE, "loadNSS: dlsym: %s\n",
|
|
|
988ad7 |
+ errmsg);
|
|
|
988ad7 |
+ handle_msg(env, msg, msg_bytes);
|
|
|
988ad7 |
+ return JNI_FALSE;
|
|
|
988ad7 |
+ }
|
|
|
988ad7 |
+ return JNI_TRUE;
|
|
|
988ad7 |
+}
|
|
|
988ad7 |
+
|
|
|
988ad7 |
+static void closeNSS(JNIEnv *env)
|
|
|
988ad7 |
+{
|
|
|
988ad7 |
+ char msg[MSG_MAX_SIZE];
|
|
|
988ad7 |
+ int msg_bytes;
|
|
|
988ad7 |
+ const char* errmsg;
|
|
|
988ad7 |
+
|
|
|
988ad7 |
+ if (dlclose(nss_handle) != 0) {
|
|
|
988ad7 |
+ errmsg = dlerror();
|
|
|
988ad7 |
+ msg_bytes = snprintf(msg, MSG_MAX_SIZE, "closeNSS: dlclose: %s\n",
|
|
|
988ad7 |
+ errmsg);
|
|
|
988ad7 |
+ handle_msg(env, msg, msg_bytes);
|
|
|
988ad7 |
+ }
|
|
|
988ad7 |
+}
|
|
|
988ad7 |
+
|
|
|
988ad7 |
+#endif
|
|
|
988ad7 |
|
|
|
988ad7 |
/*
|
|
|
988ad7 |
* Class: java_security_SystemConfigurator
|
|
|
988ad7 |
@@ -84,6 +158,14 @@ JNIEXPORT jint JNICALL DEF_JNI_OnLoad(JavaVM *vm, void *reserved)
|
|
|
988ad7 |
debugObj = (*env)->NewGlobalRef(env, debugObj);
|
|
|
988ad7 |
}
|
|
|
988ad7 |
|
|
|
988ad7 |
+#ifdef SYSCONF_NSS
|
|
|
988ad7 |
+ getSystemFIPSEnabled = *SECMOD_GetSystemFIPSEnabled;
|
|
|
988ad7 |
+#else
|
|
|
988ad7 |
+ if (loadNSS(env) == JNI_FALSE) {
|
|
|
988ad7 |
+ dbgPrint(env, "libsystemconf: Failed to load NSS library.");
|
|
|
988ad7 |
+ }
|
|
|
988ad7 |
+#endif
|
|
|
988ad7 |
+
|
|
|
988ad7 |
return (*env)->GetVersion(env);
|
|
|
988ad7 |
}
|
|
|
988ad7 |
|
|
|
988ad7 |
@@ -99,6 +181,9 @@ JNIEXPORT void JNICALL DEF_JNI_OnUnload(JavaVM *vm, void *reserved)
|
|
|
988ad7 |
if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_2) != JNI_OK) {
|
|
|
988ad7 |
return; /* Should not happen */
|
|
|
988ad7 |
}
|
|
|
988ad7 |
+#ifndef SYSCONF_NSS
|
|
|
988ad7 |
+ closeNSS(env);
|
|
|
988ad7 |
+#endif
|
|
|
988ad7 |
(*env)->DeleteGlobalRef(env, debugObj);
|
|
|
988ad7 |
}
|
|
|
988ad7 |
}
|
|
|
988ad7 |
@@ -110,61 +195,30 @@ JNIEXPORT jboolean JNICALL Java_java_security_SystemConfigurator_getSystemFIPSEn
|
|
|
988ad7 |
char msg[MSG_MAX_SIZE];
|
|
|
988ad7 |
int msg_bytes;
|
|
|
988ad7 |
|
|
|
988ad7 |
-#ifdef SYSCONF_NSS
|
|
|
988ad7 |
-
|
|
|
988ad7 |
- dbgPrint(env, "getSystemFIPSEnabled: calling SECMOD_GetSystemFIPSEnabled");
|
|
|
988ad7 |
- fips_enabled = SECMOD_GetSystemFIPSEnabled();
|
|
|
988ad7 |
- msg_bytes = snprintf(msg, MSG_MAX_SIZE, "getSystemFIPSEnabled:" \
|
|
|
988ad7 |
- " SECMOD_GetSystemFIPSEnabled returned 0x%x", fips_enabled);
|
|
|
988ad7 |
- if (msg_bytes > 0 && msg_bytes < MSG_MAX_SIZE) {
|
|
|
988ad7 |
- dbgPrint(env, msg);
|
|
|
988ad7 |
+ if (getSystemFIPSEnabled != NULL) {
|
|
|
988ad7 |
+ dbgPrint(env, "getSystemFIPSEnabled: calling SECMOD_GetSystemFIPSEnabled");
|
|
|
988ad7 |
+ fips_enabled = (*getSystemFIPSEnabled)();
|
|
|
988ad7 |
+ msg_bytes = snprintf(msg, MSG_MAX_SIZE, "getSystemFIPSEnabled:" \
|
|
|
988ad7 |
+ " SECMOD_GetSystemFIPSEnabled returned 0x%x", fips_enabled);
|
|
|
988ad7 |
+ handle_msg(env, msg, msg_bytes);
|
|
|
988ad7 |
+ return (fips_enabled == 1 ? JNI_TRUE : JNI_FALSE);
|
|
|
988ad7 |
} else {
|
|
|
988ad7 |
- dbgPrint(env, "getSystemFIPSEnabled: cannot render" \
|
|
|
988ad7 |
- " SECMOD_GetSystemFIPSEnabled return value");
|
|
|
988ad7 |
- }
|
|
|
988ad7 |
- return (fips_enabled == 1 ? JNI_TRUE : JNI_FALSE);
|
|
|
988ad7 |
-
|
|
|
988ad7 |
-#else // SYSCONF_NSS
|
|
|
988ad7 |
+ FILE *fe;
|
|
|
988ad7 |
|
|
|
988ad7 |
- FILE *fe;
|
|
|
988ad7 |
-
|
|
|
988ad7 |
- dbgPrint(env, "getSystemFIPSEnabled: reading " FIPS_ENABLED_PATH);
|
|
|
988ad7 |
- if ((fe = fopen(FIPS_ENABLED_PATH, "r")) == NULL) {
|
|
|
988ad7 |
+ dbgPrint(env, "getSystemFIPSEnabled: reading " FIPS_ENABLED_PATH);
|
|
|
988ad7 |
+ if ((fe = fopen(FIPS_ENABLED_PATH, "r")) == NULL) {
|
|
|
988ad7 |
throwIOException(env, "Cannot open " FIPS_ENABLED_PATH);
|
|
|
988ad7 |
return JNI_FALSE;
|
|
|
988ad7 |
- }
|
|
|
988ad7 |
- fips_enabled = fgetc(fe);
|
|
|
988ad7 |
- fclose(fe);
|
|
|
988ad7 |
- if (fips_enabled == EOF) {
|
|
|
988ad7 |
+ }
|
|
|
988ad7 |
+ fips_enabled = fgetc(fe);
|
|
|
988ad7 |
+ fclose(fe);
|
|
|
988ad7 |
+ if (fips_enabled == EOF) {
|
|
|
988ad7 |
throwIOException(env, "Cannot read " FIPS_ENABLED_PATH);
|
|
|
988ad7 |
return JNI_FALSE;
|
|
|
988ad7 |
- }
|
|
|
988ad7 |
- msg_bytes = snprintf(msg, MSG_MAX_SIZE, "getSystemFIPSEnabled:" \
|
|
|
988ad7 |
- " read character is '%c'", fips_enabled);
|
|
|
988ad7 |
- if (msg_bytes > 0 && msg_bytes < MSG_MAX_SIZE) {
|
|
|
988ad7 |
- dbgPrint(env, msg);
|
|
|
988ad7 |
- } else {
|
|
|
988ad7 |
- dbgPrint(env, "getSystemFIPSEnabled: cannot render" \
|
|
|
988ad7 |
- " read character");
|
|
|
988ad7 |
- }
|
|
|
988ad7 |
- return (fips_enabled == '1' ? JNI_TRUE : JNI_FALSE);
|
|
|
988ad7 |
-
|
|
|
988ad7 |
-#endif // SYSCONF_NSS
|
|
|
988ad7 |
-}
|
|
|
988ad7 |
-
|
|
|
988ad7 |
-static void throwIOException(JNIEnv *env, const char *msg)
|
|
|
988ad7 |
-{
|
|
|
988ad7 |
- jclass cls = (*env)->FindClass(env, "java/io/IOException");
|
|
|
988ad7 |
- if (cls != 0)
|
|
|
988ad7 |
- (*env)->ThrowNew(env, cls, msg);
|
|
|
988ad7 |
-}
|
|
|
988ad7 |
-
|
|
|
988ad7 |
-static void dbgPrint(JNIEnv *env, const char* msg)
|
|
|
988ad7 |
-{
|
|
|
988ad7 |
- jstring jMsg;
|
|
|
988ad7 |
- if (debugObj != NULL) {
|
|
|
988ad7 |
- jMsg = (*env)->NewStringUTF(env, msg);
|
|
|
988ad7 |
- CHECK_NULL(jMsg);
|
|
|
988ad7 |
- (*env)->CallVoidMethod(env, debugObj, debugPrintlnMethodID, jMsg);
|
|
|
988ad7 |
+ }
|
|
|
988ad7 |
+ msg_bytes = snprintf(msg, MSG_MAX_SIZE, "getSystemFIPSEnabled:" \
|
|
|
988ad7 |
+ " read character is '%c'", fips_enabled);
|
|
|
988ad7 |
+ handle_msg(env, msg, msg_bytes);
|
|
|
988ad7 |
+ return (fips_enabled == '1' ? JNI_TRUE : JNI_FALSE);
|
|
|
988ad7 |
}
|
|
|
988ad7 |
}
|