Blame SOURCES/rh1655466-global_crypto_and_fips.patch

36c706
diff --git a/src/java.base/share/classes/javopenjdk.orig///security/Security.java openjdk///src/java.base/share/classes/java/security/Security.java
36c706
--- openjdk.orig///src/java.base/share/classes/java/security/Security.java
36c706
+++ openjdk///src/java.base/share/classes/java/security/Security.java
36c706
@@ -196,26 +196,8 @@
36c706
         if (disableSystemProps == null &&
36c706
             "true".equalsIgnoreCase(props.getProperty
36c706
                 ("security.useSystemPropertiesFile"))) {
36c706
-
36c706
-            // now load the system file, if it exists, so its values
36c706
-            // will win if they conflict with the earlier values
36c706
-            try (BufferedInputStream bis =
36c706
-                 new BufferedInputStream(new FileInputStream(SYSTEM_PROPERTIES))) {
36c706
-                props.load(bis);
36c706
+            if (SystemConfigurator.configure(props)) {
36c706
                 loadedProps = true;
36c706
-
36c706
-                if (sdebug != null) {
36c706
-                    sdebug.println("reading system security properties file " +
36c706
-                                   SYSTEM_PROPERTIES);
36c706
-                    sdebug.println(props.toString());
36c706
-                }
36c706
-            } catch (IOException e) {
36c706
-                if (sdebug != null) {
36c706
-                    sdebug.println
36c706
-                        ("unable to load security properties from " +
36c706
-                         SYSTEM_PROPERTIES);
36c706
-                    e.printStackTrace();
36c706
-                }
36c706
             }
36c706
         }
36c706
 
36c706
diff --git a/src/java.base/share/classes/javopenjdk.orig///security/SystemConfigurator.java openjdk///src/java.base/share/classes/java/security/SystemConfigurator.java
36c706
new file mode 100644
36c706
--- /dev/null
36c706
+++ openjdk///src/java.base/share/classes/java/security/SystemConfigurator.java
36c706
@@ -0,0 +1,151 @@
36c706
+/*
36c706
+ * Copyright (c) 2019, Red Hat, Inc.
36c706
+ *
36c706
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
36c706
+ *
36c706
+ * This code is free software; you can redistribute it and/or modify it
36c706
+ * under the terms of the GNU General Public License version 2 only, as
36c706
+ * published by the Free Software Foundation.
36c706
+ *
36c706
+ * This code is distributed in the hope that it will be useful, but WITHOUT
36c706
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
36c706
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
36c706
+ * version 2 for more details (a copy is included in the LICENSE file that
36c706
+ * accompanied this code).
36c706
+ *
36c706
+ * You should have received a copy of the GNU General Public License version
36c706
+ * 2 along with this work; if not, write to the Free Software Foundation,
36c706
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
36c706
+ *
36c706
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
36c706
+ * or visit www.oracle.com if you need additional information or have any
36c706
+ * questions.
36c706
+ */
36c706
+
36c706
+package java.security;
36c706
+
36c706
+import java.io.BufferedInputStream;
36c706
+import java.io.FileInputStream;
36c706
+import java.io.IOException;
36c706
+
36c706
+import java.nio.file.Files;
36c706
+import java.nio.file.Path;
36c706
+
36c706
+import java.util.Iterator;
36c706
+import java.util.Map.Entry;
36c706
+import java.util.Properties;
36c706
+import java.util.function.Consumer;
36c706
+import java.util.regex.Matcher;
36c706
+import java.util.regex.Pattern;
36c706
+
36c706
+import sun.security.util.Debug;
36c706
+
36c706
+/**
36c706
+ * Internal class to align OpenJDK with global crypto-policies.
36c706
+ * Called from java.security.Security class initialization,
36c706
+ * during startup.
36c706
+ *
36c706
+ */
36c706
+
36c706
+class SystemConfigurator {
36c706
+
36c706
+    private static final Debug sdebug =
36c706
+            Debug.getInstance("properties");
36c706
+
36c706
+    private static final String CRYPTO_POLICIES_BASE_DIR =
36c706
+            "/etc/crypto-policies";
36c706
+
36c706
+    private static final String CRYPTO_POLICIES_JAVA_CONFIG =
36c706
+            CRYPTO_POLICIES_BASE_DIR + "/back-ends/java.config";
36c706
+
36c706
+    private static final String CRYPTO_POLICIES_CONFIG =
36c706
+            CRYPTO_POLICIES_BASE_DIR + "/config";
36c706
+
36c706
+    private static final class SecurityProviderInfo {
36c706
+        int number;
36c706
+        String key;
36c706
+        String value;
36c706
+        SecurityProviderInfo(int number, String key, String value) {
36c706
+            this.number = number;
36c706
+            this.key = key;
36c706
+            this.value = value;
36c706
+        }
36c706
+    }
36c706
+
36c706
+    /*
36c706
+     * Invoked when java.security.Security class is initialized, if
36c706
+     * java.security.disableSystemPropertiesFile property is not set and
36c706
+     * security.useSystemPropertiesFile is true.
36c706
+     */
36c706
+    static boolean configure(Properties props) {
36c706
+        boolean loadedProps = false;
36c706
+
36c706
+        try (BufferedInputStream bis =
36c706
+                new BufferedInputStream(
36c706
+                        new FileInputStream(CRYPTO_POLICIES_JAVA_CONFIG))) {
36c706
+            props.load(bis);
36c706
+            loadedProps = true;
36c706
+            if (sdebug != null) {
36c706
+                sdebug.println("reading system security properties file " +
36c706
+                        CRYPTO_POLICIES_JAVA_CONFIG);
36c706
+                sdebug.println(props.toString());
36c706
+            }
36c706
+        } catch (IOException e) {
36c706
+            if (sdebug != null) {
36c706
+                sdebug.println("unable to load security properties from " +
36c706
+                        CRYPTO_POLICIES_JAVA_CONFIG);
36c706
+                e.printStackTrace();
36c706
+            }
36c706
+        }
36c706
+
36c706
+        try {
36c706
+            if (enableFips()) {
36c706
+                if (sdebug != null) { sdebug.println("FIPS mode detected"); }
36c706
+                loadedProps = false;
36c706
+                // Remove all security providers
36c706
+                Iterator<Entry<Object, Object>> i = props.entrySet().iterator();
36c706
+                while (i.hasNext()) {
36c706
+                    Entry<Object, Object> e = i.next();
36c706
+                    if (((String) e.getKey()).startsWith("security.provider")) {
36c706
+                        if (sdebug != null) { sdebug.println("Removing provider: " + e); }
36c706
+                        i.remove();
36c706
+                    }
36c706
+                }
36c706
+                // Add FIPS security providers
36c706
+                String fipsProviderValue = null;
36c706
+                for (int n = 1;
36c706
+                     (fipsProviderValue = (String) props.get("fips.provider." + n)) != null; n++) {
36c706
+                    String fipsProviderKey = "security.provider." + n;
36c706
+                    if (sdebug != null) {
36c706
+                        sdebug.println("Adding provider " + n + ": " +
36c706
+                                fipsProviderKey + "=" + fipsProviderValue);
36c706
+                    }
36c706
+                    props.put(fipsProviderKey, fipsProviderValue);
36c706
+                }
36c706
+                loadedProps = true;
36c706
+            }
36c706
+        } catch (Exception e) {
36c706
+            if (sdebug != null) {
36c706
+                sdebug.println("unable to load FIPS configuration");
36c706
+                e.printStackTrace();
36c706
+            }
36c706
+        }
36c706
+        return loadedProps;
36c706
+    }
36c706
+
36c706
+    /*
36c706
+     * FIPS is enabled only if crypto-policies are set to "FIPS"
36c706
+     * and the com.redhat.fips property is true.
36c706
+     */
36c706
+    private static boolean enableFips() throws Exception {
36c706
+        boolean fipsEnabled = Boolean.valueOf(System.getProperty("com.redhat.fips", "false"));
36c706
+        if (fipsEnabled) {
36c706
+            String cryptoPoliciesConfig = new String(Files.readAllBytes(Path.of(CRYPTO_POLICIES_CONFIG)));
36c706
+            if (sdebug != null) { sdebug.println("Crypto config:\n" + cryptoPoliciesConfig); }
36c706
+            Pattern pattern = Pattern.compile("^FIPS$", Pattern.MULTILINE);
36c706
+            return pattern.matcher(cryptoPoliciesConfig).find();
36c706
+        } else {
36c706
+            return false;
36c706
+        }
36c706
+    }
36c706
+}
36c706
diff --git openjdk.orig///src/java.base/share/conf/security/java.security openjdk///src/java.base/share/conf/security/java.security
36c706
--- openjdk.orig///src/java.base/share/conf/security/java.security
36c706
+++ openjdk///src/java.base/share/conf/security/java.security
36c706
@@ -87,6 +87,14 @@
36c706
 #security.provider.tbd=SunPKCS11 ${java.home}/lib/security/nss.cfg
36c706
 
36c706
 #
36c706
+# Security providers used when global crypto-policies are set to FIPS.
36c706
+#
36c706
+fips.provider.1=SunPKCS11 ${java.home}/conf/security/nss.fips.cfg
36c706
+fips.provider.2=SUN
36c706
+fips.provider.3=SunEC
36c706
+fips.provider.4=com.sun.net.ssl.internal.ssl.Provider SunPKCS11-NSS-FIPS
36c706
+
36c706
+#
36c706
 # A list of preferred providers for specific algorithms. These providers will
36c706
 # be searched for matching algorithms before the list of registered providers.
36c706
 # Entries containing errors (parsing, etc) will be ignored. Use the