Blame SOURCES/rh1929465-improve_system_FIPS_detection-jdk.patch

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