Blame SOURCES/rh1929465-improve_system_FIPS_detection-jdk.patch

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