Blame SOURCES/rh2052829-fips_runtime_nss_detection.patch

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