Blame SOURCES/rh1929465-improve_system_FIPS_detection-jdk.patch

5b7429
diff --git openjdk.orig/jdk/make/lib/SecurityLibraries.gmk openjdk/jdk/make/lib/SecurityLibraries.gmk
5b7429
--- openjdk.orig/jdk/make/lib/SecurityLibraries.gmk
5b7429
+++ openjdk/jdk/make/lib/SecurityLibraries.gmk
5b7429
@@ -289,3 +289,34 @@
5b7429
 
5b7429
   endif
5b7429
 endif
5b7429
+
5b7429
+################################################################################
5b7429
+# Create the systemconf library
5b7429
+
5b7429
+LIBSYSTEMCONF_CFLAGS :=
5b7429
+LIBSYSTEMCONF_CXXFLAGS :=
5b7429
+
5b7429
+ifeq ($(USE_SYSCONF_NSS), true)
5b7429
+  LIBSYSTEMCONF_CFLAGS += $(NSS_CFLAGS) -DSYSCONF_NSS
5b7429
+  LIBSYSTEMCONF_CXXFLAGS += $(NSS_CFLAGS) -DSYSCONF_NSS
5b7429
+endif
5b7429
+
5b7429
+ifeq ($(OPENJDK_BUILD_OS), linux)
5b7429
+  $(eval $(call SetupNativeCompilation,BUILD_LIBSYSTEMCONF, \
5b7429
+      LIBRARY := systemconf, \
5b7429
+      OUTPUT_DIR := $(INSTALL_LIBRARIES_HERE), \
5b7429
+      SRC := $(JDK_TOPDIR)/src/$(OPENJDK_TARGET_OS_API_DIR)/native/java/security, \
5b7429
+      LANG := C, \
5b7429
+      OPTIMIZATION := LOW, \
5b7429
+      CFLAGS := $(CFLAGS_JDKLIB) $(LIBSYSTEMCONF_CFLAGS), \
5b7429
+      CXXFLAGS := $(CXXFLAGS_JDKLIB) $(LIBSYSTEMCONF_CXXFLAGS), \
5b7429
+      MAPFILE := $(JDK_TOPDIR)/make/mapfiles/libsystemconf/mapfile-vers, \
5b7429
+      LDFLAGS := $(LDFLAGS_JDKLIB) \
5b7429
+          $(call SET_SHARED_LIBRARY_ORIGIN), \
5b7429
+      LDFLAGS_SUFFIX := $(LIBDL) $(NSS_LIBS), \
5b7429
+      OBJECT_DIR := $(JDK_OUTPUTDIR)/objs/libsystemconf, \
5b7429
+      DEBUG_SYMBOLS := $(DEBUG_ALL_BINARIES)))
5b7429
+
5b7429
+  BUILD_LIBRARIES += $(BUILD_LIBSYSTEMCONF)
5b7429
+endif
5b7429
+
5b7429
diff --git openjdk.orig/jdk/make/mapfiles/libsystemconf/mapfile-vers openjdk/jdk/make/mapfiles/libsystemconf/mapfile-vers
5b7429
new file mode 100644
5b7429
--- /dev/null
5b7429
+++ openjdk/jdk/make/mapfiles/libsystemconf/mapfile-vers
5b7429
@@ -0,0 +1,35 @@
5b7429
+#
5b7429
+# Copyright (c) 2021, Red Hat, Inc.
5b7429
+# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5b7429
+#
5b7429
+# This code is free software; you can redistribute it and/or modify it
5b7429
+# under the terms of the GNU General Public License version 2 only, as
5b7429
+# published by the Free Software Foundation.  Oracle designates this
5b7429
+# particular file as subject to the "Classpath" exception as provided
5b7429
+# by Oracle in the LICENSE file that accompanied this code.
5b7429
+#
5b7429
+# This code is distributed in the hope that it will be useful, but WITHOUT
5b7429
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5b7429
+# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5b7429
+# version 2 for more details (a copy is included in the LICENSE file that
5b7429
+# accompanied this code).
5b7429
+#
5b7429
+# You should have received a copy of the GNU General Public License version
5b7429
+# 2 along with this work; if not, write to the Free Software Foundation,
5b7429
+# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5b7429
+#
5b7429
+# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5b7429
+# or visit www.oracle.com if you need additional information or have any
5b7429
+# questions.
5b7429
+#
5b7429
+
5b7429
+# Define public interface.
5b7429
+
5b7429
+SUNWprivate_1.1 {
5b7429
+	global:
5b7429
+		DEF_JNI_OnLoad;
5b7429
+		DEF_JNI_OnUnLoad;
5b7429
+		Java_java_security_SystemConfigurator_getSystemFIPSEnabled;
5b7429
+	local:
5b7429
+		*;
5b7429
+};
5b7429
diff --git openjdk.orig/jdk/src/share/classes/java/security/SystemConfigurator.java openjdk/jdk/src/share/classes/java/security/SystemConfigurator.java
5b7429
--- openjdk.orig/jdk/src/share/classes/java/security/SystemConfigurator.java
5b7429
+++ openjdk/jdk/src/share/classes/java/security/SystemConfigurator.java
5b7429
@@ -1,5 +1,5 @@
5b7429
 /*
5b7429
- * Copyright (c) 2019, 2020, Red Hat, Inc.
5b7429
+ * Copyright (c) 2019, 2021, Red Hat, Inc.
5b7429
  *
5b7429
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5b7429
  *
5b7429
@@ -30,14 +30,9 @@
5b7429
 import java.io.FileInputStream;
5b7429
 import java.io.IOException;
5b7429
 
5b7429
-import java.nio.file.Files;
5b7429
-import java.nio.file.FileSystems;
5b7429
-import java.nio.file.Path;
5b7429
-
5b7429
 import java.util.Iterator;
5b7429
 import java.util.Map.Entry;
5b7429
 import java.util.Properties;
5b7429
-import java.util.regex.Pattern;
5b7429
 
5b7429
 import sun.security.util.Debug;
5b7429
 
5b7429
@@ -59,10 +54,21 @@
5b7429
     private static final String CRYPTO_POLICIES_JAVA_CONFIG =
5b7429
             CRYPTO_POLICIES_BASE_DIR + "/back-ends/java.config";
5b7429
 
5b7429
-    private static final String CRYPTO_POLICIES_CONFIG =
5b7429
-            CRYPTO_POLICIES_BASE_DIR + "/config";
5b7429
+    private static boolean systemFipsEnabled = false;
5b7429
+
5b7429
+    private static final String SYSTEMCONF_NATIVE_LIB = "systemconf";
5b7429
+
5b7429
+    private static native boolean getSystemFIPSEnabled()
5b7429
+            throws IOException;
5b7429
 
5b7429
-    private static boolean systemFipsEnabled = false;
5b7429
+    static {
5b7429
+        AccessController.doPrivileged(new PrivilegedAction<Void>() {
5b7429
+            public Void run() {
5b7429
+                System.loadLibrary(SYSTEMCONF_NATIVE_LIB);
5b7429
+                return null;
5b7429
+            }
5b7429
+        });
5b7429
+    }
5b7429
 
5b7429
     /*
5b7429
      * Invoked when java.security.Security class is initialized, if
5b7429
@@ -171,17 +177,34 @@
5b7429
     }
5b7429
 
5b7429
     /*
5b7429
-     * FIPS is enabled only if crypto-policies are set to "FIPS"
5b7429
-     * and the com.redhat.fips property is true.
5b7429
+     * OpenJDK FIPS mode will be enabled only if the com.redhat.fips
5b7429
+     * system property is true (default) and the system is in FIPS mode.
5b7429
+     *
5b7429
+     * There are 2 possible ways in which OpenJDK detects that the system
5b7429
+     * is in FIPS mode: 1) if the NSS SECMOD_GetSystemFIPSEnabled API is
5b7429
+     * available at OpenJDK's built-time, it is called; 2) otherwise, the
5b7429
+     * /proc/sys/crypto/fips_enabled file is read.
5b7429
      */
5b7429
-    private static boolean enableFips() throws Exception {
5b7429
+    private static boolean enableFips() throws IOException {
5b7429
         boolean shouldEnable = Boolean.valueOf(System.getProperty("com.redhat.fips", "true"));
5b7429
         if (shouldEnable) {
5b7429
-            Path configPath = FileSystems.getDefault().getPath(CRYPTO_POLICIES_CONFIG);
5b7429
-            String cryptoPoliciesConfig = new String(Files.readAllBytes(configPath));
5b7429
-            if (sdebug != null) { sdebug.println("Crypto config:\n" + cryptoPoliciesConfig); }
5b7429
-            Pattern pattern = Pattern.compile("^FIPS$", Pattern.MULTILINE);
5b7429
-            return pattern.matcher(cryptoPoliciesConfig).find();
5b7429
+            if (sdebug != null) {
5b7429
+                sdebug.println("Calling getSystemFIPSEnabled (libsystemconf)...");
5b7429
+            }
5b7429
+            try {
5b7429
+                shouldEnable = getSystemFIPSEnabled();
5b7429
+                if (sdebug != null) {
5b7429
+                    sdebug.println("Call to getSystemFIPSEnabled (libsystemconf) returned: "
5b7429
+                            + shouldEnable);
5b7429
+                }
5b7429
+                return shouldEnable;
5b7429
+            } catch (IOException e) {
5b7429
+                if (sdebug != null) {
5b7429
+                    sdebug.println("Call to getSystemFIPSEnabled (libsystemconf) failed:");
5b7429
+                    sdebug.println(e.getMessage());
5b7429
+                }
5b7429
+                throw e;
5b7429
+            }
5b7429
         } else {
5b7429
             return false;
5b7429
         }
5b7429
diff --git openjdk.orig/jdk/src/solaris/native/java/security/systemconf.c openjdk/jdk/src/solaris/native/java/security/systemconf.c
5b7429
new file mode 100644
5b7429
--- /dev/null
5b7429
+++ openjdk/jdk/src/solaris/native/java/security/systemconf.c
5b7429
@@ -0,0 +1,168 @@
5b7429
+/*
5b7429
+ * Copyright (c) 2021, Red Hat, Inc.
5b7429
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5b7429
+ *
5b7429
+ * This code is free software; you can redistribute it and/or modify it
5b7429
+ * under the terms of the GNU General Public License version 2 only, as
5b7429
+ * published by the Free Software Foundation.  Oracle designates this
5b7429
+ * particular file as subject to the "Classpath" exception as provided
5b7429
+ * by Oracle in the LICENSE file that accompanied this code.
5b7429
+ *
5b7429
+ * This code is distributed in the hope that it will be useful, but WITHOUT
5b7429
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5b7429
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5b7429
+ * version 2 for more details (a copy is included in the LICENSE file that
5b7429
+ * accompanied this code).
5b7429
+ *
5b7429
+ * You should have received a copy of the GNU General Public License version
5b7429
+ * 2 along with this work; if not, write to the Free Software Foundation,
5b7429
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5b7429
+ *
5b7429
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5b7429
+ * or visit www.oracle.com if you need additional information or have any
5b7429
+ * questions.
5b7429
+ */
5b7429
+
5b7429
+#include <dlfcn.h>
5b7429
+#include <jni.h>
5b7429
+#include <jni_util.h>
5b7429
+#include <stdio.h>
5b7429
+
5b7429
+#ifdef SYSCONF_NSS
5b7429
+#include <nss3/pk11pub.h>
5b7429
+#endif //SYSCONF_NSS
5b7429
+
5b7429
+#include "java_security_SystemConfigurator.h"
5b7429
+
5b7429
+#define FIPS_ENABLED_PATH "/proc/sys/crypto/fips_enabled"
5b7429
+#define MSG_MAX_SIZE 96
5b7429
+
5b7429
+static jmethodID debugPrintlnMethodID = NULL;
5b7429
+static jobject debugObj = NULL;
5b7429
+
5b7429
+static void throwIOException(JNIEnv *env, const char *msg);
5b7429
+static void dbgPrint(JNIEnv *env, const char* msg);
5b7429
+
5b7429
+/*
5b7429
+ * Class:     java_security_SystemConfigurator
5b7429
+ * Method:    JNI_OnLoad
5b7429
+ */
5b7429
+JNIEXPORT jint JNICALL DEF_JNI_OnLoad(JavaVM *vm, void *reserved)
5b7429
+{
5b7429
+    JNIEnv *env;
5b7429
+    jclass sysConfCls, debugCls;
5b7429
+    jfieldID sdebugFld;
5b7429
+
5b7429
+    if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_2) != JNI_OK) {
5b7429
+        return JNI_EVERSION; /* JNI version not supported */
5b7429
+    }
5b7429
+
5b7429
+    sysConfCls = (*env)->FindClass(env,"java/security/SystemConfigurator");
5b7429
+    if (sysConfCls == NULL) {
5b7429
+        printf("libsystemconf: SystemConfigurator class not found\n");
5b7429
+        return JNI_ERR;
5b7429
+    }
5b7429
+    sdebugFld = (*env)->GetStaticFieldID(env, sysConfCls,
5b7429
+            "sdebug", "Lsun/security/util/Debug;");
5b7429
+    if (sdebugFld == NULL) {
5b7429
+        printf("libsystemconf: SystemConfigurator::sdebug field not found\n");
5b7429
+        return JNI_ERR;
5b7429
+    }
5b7429
+    debugObj = (*env)->GetStaticObjectField(env, sysConfCls, sdebugFld);
5b7429
+    if (debugObj != NULL) {
5b7429
+        debugCls = (*env)->FindClass(env,"sun/security/util/Debug");
5b7429
+        if (debugCls == NULL) {
5b7429
+            printf("libsystemconf: Debug class not found\n");
5b7429
+            return JNI_ERR;
5b7429
+        }
5b7429
+        debugPrintlnMethodID = (*env)->GetMethodID(env, debugCls,
5b7429
+                "println", "(Ljava/lang/String;)V");
5b7429
+        if (debugPrintlnMethodID == NULL) {
5b7429
+            printf("libsystemconf: Debug::println(String) method not found\n");
5b7429
+            return JNI_ERR;
5b7429
+        }
5b7429
+        debugObj = (*env)->NewGlobalRef(env, debugObj);
5b7429
+    }
5b7429
+
5b7429
+    return (*env)->GetVersion(env);
5b7429
+}
5b7429
+
5b7429
+/*
5b7429
+ * Class:     java_security_SystemConfigurator
5b7429
+ * Method:    JNI_OnUnload
5b7429
+ */
5b7429
+JNIEXPORT void JNICALL DEF_JNI_OnUnload(JavaVM *vm, void *reserved)
5b7429
+{
5b7429
+    JNIEnv *env;
5b7429
+
5b7429
+    if (debugObj != NULL) {
5b7429
+        if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_2) != JNI_OK) {
5b7429
+            return; /* Should not happen */
5b7429
+        }
5b7429
+        (*env)->DeleteGlobalRef(env, debugObj);
5b7429
+    }
5b7429
+}
5b7429
+
5b7429
+JNIEXPORT jboolean JNICALL Java_java_security_SystemConfigurator_getSystemFIPSEnabled
5b7429
+  (JNIEnv *env, jclass cls)
5b7429
+{
5b7429
+    int fips_enabled;
5b7429
+    char msg[MSG_MAX_SIZE];
5b7429
+    int msg_bytes;
5b7429
+
5b7429
+#ifdef SYSCONF_NSS
5b7429
+
5b7429
+    dbgPrint(env, "getSystemFIPSEnabled: calling SECMOD_GetSystemFIPSEnabled");
5b7429
+    fips_enabled = SECMOD_GetSystemFIPSEnabled();
5b7429
+    msg_bytes = snprintf(msg, MSG_MAX_SIZE, "getSystemFIPSEnabled:" \
5b7429
+            " SECMOD_GetSystemFIPSEnabled returned 0x%x", fips_enabled);
5b7429
+    if (msg_bytes > 0 && msg_bytes < MSG_MAX_SIZE) {
5b7429
+        dbgPrint(env, msg);
5b7429
+    } else {
5b7429
+        dbgPrint(env, "getSystemFIPSEnabled: cannot render" \
5b7429
+                " SECMOD_GetSystemFIPSEnabled return value");
5b7429
+    }
5b7429
+    return (fips_enabled == 1 ? JNI_TRUE : JNI_FALSE);
5b7429
+
5b7429
+#else // SYSCONF_NSS
5b7429
+
5b7429
+    FILE *fe;
5b7429
+
5b7429
+    dbgPrint(env, "getSystemFIPSEnabled: reading " FIPS_ENABLED_PATH);
5b7429
+    if ((fe = fopen(FIPS_ENABLED_PATH, "r")) == NULL) {
5b7429
+        throwIOException(env, "Cannot open " FIPS_ENABLED_PATH);
5b7429
+    }
5b7429
+    fips_enabled = fgetc(fe);
5b7429
+    fclose(fe);
5b7429
+    if (fips_enabled == EOF) {
5b7429
+        throwIOException(env, "Cannot read " FIPS_ENABLED_PATH);
5b7429
+    }
5b7429
+    msg_bytes = snprintf(msg, MSG_MAX_SIZE, "getSystemFIPSEnabled:" \
5b7429
+            " read character is '%c'", fips_enabled);
5b7429
+    if (msg_bytes > 0 && msg_bytes < MSG_MAX_SIZE) {
5b7429
+        dbgPrint(env, msg);
5b7429
+    } else {
5b7429
+        dbgPrint(env, "getSystemFIPSEnabled: cannot render" \
5b7429
+                " read character");
5b7429
+    }
5b7429
+    return (fips_enabled == '1' ? JNI_TRUE : JNI_FALSE);
5b7429
+
5b7429
+#endif // SYSCONF_NSS
5b7429
+}
5b7429
+
5b7429
+static void throwIOException(JNIEnv *env, const char *msg)
5b7429
+{
5b7429
+    jclass cls = (*env)->FindClass(env, "java/io/IOException");
5b7429
+    if (cls != 0)
5b7429
+        (*env)->ThrowNew(env, cls, msg);
5b7429
+}
5b7429
+
5b7429
+static void dbgPrint(JNIEnv *env, const char* msg)
5b7429
+{
5b7429
+    jstring jMsg;
5b7429
+    if (debugObj != NULL) {
5b7429
+        jMsg = (*env)->NewStringUTF(env, msg);
5b7429
+        CHECK_NULL(jMsg);
5b7429
+        (*env)->CallVoidMethod(env, debugObj, debugPrintlnMethodID, jMsg);
5b7429
+    }
5b7429
+}