Blame SOURCES/rh1929465-improve_system_FIPS_detection-jdk.patch

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