Blame SOURCES/rh2052829-fips_runtime_nss_detection.patch

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